Different round behaviour for Python round on float and numpy.float64
I stumpled into a test failure caused by floating point precision and am trying to understand it.
In short: Python3 round
returns a different value depending on whether the type is a float or a numpy.float64 although I thought float==double==float64
and both Python3 and NumPy should round nearest to even.
Here the example:
npVal = np.float64(435)/100
pyVal = 435/100
print(round(npVal,1)) // 4.4
print(round(pyVal,1)) // 4.3
print(round(np.float64(pyVal),1)) // 4.4
print(round(float(npVal),1)) // 4.3
I understand that 4.35 and 4.4 might not be exactly representable in double but why is numpy round differently than Python although they both use the same datatypes and specify the function similar? I used the explicit division to avoid input rounding errors.
I don't know for sure, whether the double value for 4.35 is a bit more or less, so I can't say which of those implementations is (might be?) wrong.
There is a similar question: Strange behavior of numpy.round
There it was noted, that NumPy "rounds to the nearest even value" and "behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here".
So both should do the same and round to nearest even value. So if 4.35 would be an exact float, 4.4 would the correct answer and needed to be returned by both.
python numpy floating-point rounding
|
show 1 more comment
I stumpled into a test failure caused by floating point precision and am trying to understand it.
In short: Python3 round
returns a different value depending on whether the type is a float or a numpy.float64 although I thought float==double==float64
and both Python3 and NumPy should round nearest to even.
Here the example:
npVal = np.float64(435)/100
pyVal = 435/100
print(round(npVal,1)) // 4.4
print(round(pyVal,1)) // 4.3
print(round(np.float64(pyVal),1)) // 4.4
print(round(float(npVal),1)) // 4.3
I understand that 4.35 and 4.4 might not be exactly representable in double but why is numpy round differently than Python although they both use the same datatypes and specify the function similar? I used the explicit division to avoid input rounding errors.
I don't know for sure, whether the double value for 4.35 is a bit more or less, so I can't say which of those implementations is (might be?) wrong.
There is a similar question: Strange behavior of numpy.round
There it was noted, that NumPy "rounds to the nearest even value" and "behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here".
So both should do the same and round to nearest even value. So if 4.35 would be an exact float, 4.4 would the correct answer and needed to be returned by both.
python numpy floating-point rounding
They are both right,4.35
is the same distance on the number line from4.4
as it is from4.3
. You could try allowing a higher number of decimals to kick the can a little farther down the road.
– vencaslac
Nov 12 '18 at 18:05
Both implementations ofround
should round to even as explained in a comment to the linked question: "It's worth noting that this behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here." I'm asking why this is not the case?
– Flamefire
Nov 12 '18 at 18:16
1
@coldspeed This isn't a duplicate: the other question was about Python 2, this question uses aprint
function, so is using Python 3, where they should have the same behaviour.
– Simon Byrne
Nov 12 '18 at 19:00
2
Python'sround
is correctly-rounded (but slow). NumPy's is not (but is faster). Different tradeoffs. (Agreed that this isn't a duplicate.)
– Mark Dickinson
Nov 12 '18 at 19:01
@coldspeed: That question is not a duplicate. It saysnumpy.round
rounds to even for values exactly halfway between rounded decimal values. But thenpVal
in this question cannot be exactly halfway between 4.3 and 4.4 because 4.35 is not representable in 64-bit binary floating-point. The closest representable value is 4.3499999999999996447286321199499070644378662109375, which ought to round down.
– Eric Postpischil
Nov 12 '18 at 19:15
|
show 1 more comment
I stumpled into a test failure caused by floating point precision and am trying to understand it.
In short: Python3 round
returns a different value depending on whether the type is a float or a numpy.float64 although I thought float==double==float64
and both Python3 and NumPy should round nearest to even.
Here the example:
npVal = np.float64(435)/100
pyVal = 435/100
print(round(npVal,1)) // 4.4
print(round(pyVal,1)) // 4.3
print(round(np.float64(pyVal),1)) // 4.4
print(round(float(npVal),1)) // 4.3
I understand that 4.35 and 4.4 might not be exactly representable in double but why is numpy round differently than Python although they both use the same datatypes and specify the function similar? I used the explicit division to avoid input rounding errors.
I don't know for sure, whether the double value for 4.35 is a bit more or less, so I can't say which of those implementations is (might be?) wrong.
There is a similar question: Strange behavior of numpy.round
There it was noted, that NumPy "rounds to the nearest even value" and "behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here".
So both should do the same and round to nearest even value. So if 4.35 would be an exact float, 4.4 would the correct answer and needed to be returned by both.
python numpy floating-point rounding
I stumpled into a test failure caused by floating point precision and am trying to understand it.
In short: Python3 round
returns a different value depending on whether the type is a float or a numpy.float64 although I thought float==double==float64
and both Python3 and NumPy should round nearest to even.
Here the example:
npVal = np.float64(435)/100
pyVal = 435/100
print(round(npVal,1)) // 4.4
print(round(pyVal,1)) // 4.3
print(round(np.float64(pyVal),1)) // 4.4
print(round(float(npVal),1)) // 4.3
I understand that 4.35 and 4.4 might not be exactly representable in double but why is numpy round differently than Python although they both use the same datatypes and specify the function similar? I used the explicit division to avoid input rounding errors.
I don't know for sure, whether the double value for 4.35 is a bit more or less, so I can't say which of those implementations is (might be?) wrong.
There is a similar question: Strange behavior of numpy.round
There it was noted, that NumPy "rounds to the nearest even value" and "behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here".
So both should do the same and round to nearest even value. So if 4.35 would be an exact float, 4.4 would the correct answer and needed to be returned by both.
python numpy floating-point rounding
python numpy floating-point rounding
edited Nov 13 '18 at 9:12
Flamefire
asked Nov 12 '18 at 18:02
FlamefireFlamefire
1,4271126
1,4271126
They are both right,4.35
is the same distance on the number line from4.4
as it is from4.3
. You could try allowing a higher number of decimals to kick the can a little farther down the road.
– vencaslac
Nov 12 '18 at 18:05
Both implementations ofround
should round to even as explained in a comment to the linked question: "It's worth noting that this behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here." I'm asking why this is not the case?
– Flamefire
Nov 12 '18 at 18:16
1
@coldspeed This isn't a duplicate: the other question was about Python 2, this question uses aprint
function, so is using Python 3, where they should have the same behaviour.
– Simon Byrne
Nov 12 '18 at 19:00
2
Python'sround
is correctly-rounded (but slow). NumPy's is not (but is faster). Different tradeoffs. (Agreed that this isn't a duplicate.)
– Mark Dickinson
Nov 12 '18 at 19:01
@coldspeed: That question is not a duplicate. It saysnumpy.round
rounds to even for values exactly halfway between rounded decimal values. But thenpVal
in this question cannot be exactly halfway between 4.3 and 4.4 because 4.35 is not representable in 64-bit binary floating-point. The closest representable value is 4.3499999999999996447286321199499070644378662109375, which ought to round down.
– Eric Postpischil
Nov 12 '18 at 19:15
|
show 1 more comment
They are both right,4.35
is the same distance on the number line from4.4
as it is from4.3
. You could try allowing a higher number of decimals to kick the can a little farther down the road.
– vencaslac
Nov 12 '18 at 18:05
Both implementations ofround
should round to even as explained in a comment to the linked question: "It's worth noting that this behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here." I'm asking why this is not the case?
– Flamefire
Nov 12 '18 at 18:16
1
@coldspeed This isn't a duplicate: the other question was about Python 2, this question uses aprint
function, so is using Python 3, where they should have the same behaviour.
– Simon Byrne
Nov 12 '18 at 19:00
2
Python'sround
is correctly-rounded (but slow). NumPy's is not (but is faster). Different tradeoffs. (Agreed that this isn't a duplicate.)
– Mark Dickinson
Nov 12 '18 at 19:01
@coldspeed: That question is not a duplicate. It saysnumpy.round
rounds to even for values exactly halfway between rounded decimal values. But thenpVal
in this question cannot be exactly halfway between 4.3 and 4.4 because 4.35 is not representable in 64-bit binary floating-point. The closest representable value is 4.3499999999999996447286321199499070644378662109375, which ought to round down.
– Eric Postpischil
Nov 12 '18 at 19:15
They are both right,
4.35
is the same distance on the number line from 4.4
as it is from 4.3
. You could try allowing a higher number of decimals to kick the can a little farther down the road.– vencaslac
Nov 12 '18 at 18:05
They are both right,
4.35
is the same distance on the number line from 4.4
as it is from 4.3
. You could try allowing a higher number of decimals to kick the can a little farther down the road.– vencaslac
Nov 12 '18 at 18:05
Both implementations of
round
should round to even as explained in a comment to the linked question: "It's worth noting that this behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here." I'm asking why this is not the case?– Flamefire
Nov 12 '18 at 18:16
Both implementations of
round
should round to even as explained in a comment to the linked question: "It's worth noting that this behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here." I'm asking why this is not the case?– Flamefire
Nov 12 '18 at 18:16
1
1
@coldspeed This isn't a duplicate: the other question was about Python 2, this question uses a
print
function, so is using Python 3, where they should have the same behaviour.– Simon Byrne
Nov 12 '18 at 19:00
@coldspeed This isn't a duplicate: the other question was about Python 2, this question uses a
print
function, so is using Python 3, where they should have the same behaviour.– Simon Byrne
Nov 12 '18 at 19:00
2
2
Python's
round
is correctly-rounded (but slow). NumPy's is not (but is faster). Different tradeoffs. (Agreed that this isn't a duplicate.)– Mark Dickinson
Nov 12 '18 at 19:01
Python's
round
is correctly-rounded (but slow). NumPy's is not (but is faster). Different tradeoffs. (Agreed that this isn't a duplicate.)– Mark Dickinson
Nov 12 '18 at 19:01
@coldspeed: That question is not a duplicate. It says
numpy.round
rounds to even for values exactly halfway between rounded decimal values. But the npVal
in this question cannot be exactly halfway between 4.3 and 4.4 because 4.35 is not representable in 64-bit binary floating-point. The closest representable value is 4.3499999999999996447286321199499070644378662109375, which ought to round down.– Eric Postpischil
Nov 12 '18 at 19:15
@coldspeed: That question is not a duplicate. It says
numpy.round
rounds to even for values exactly halfway between rounded decimal values. But the npVal
in this question cannot be exactly halfway between 4.3 and 4.4 because 4.35 is not representable in 64-bit binary floating-point. The closest representable value is 4.3499999999999996447286321199499070644378662109375, which ought to round down.– Eric Postpischil
Nov 12 '18 at 19:15
|
show 1 more comment
1 Answer
1
active
oldest
votes
Calculating 435/100 in IEEE-754 basic 64-bit binary floating-point yields 4.3499999999999996447286321199499070644378662109375.
When this is rounded to the nearest decimal numeral with one digit after the decimal point, the result ought to be “4.3”. The Python rounding for this case appears to be correct.
For numpy.round
, the documentation refers to numpy.around
. The documentation for that says “Results may also be surprising due to … errors introduced when scaling by powers of ten.” Thus, it may be that numpy.round
does not calculate the correct conversion of 4.3499999999999996447286321199499070644378662109375 to decimal but rather performs a 64-bit binary floating-point multiplication of that by 10, which yields exactly 43.5 due to floating-point rounding, and then numpy.round
rounds that to 44 and formats it as “4.4”.
In summary, numpy.round
is not a correct rounding routine.
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’sprintf
andscanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.
– Eric Postpischil
Nov 13 '18 at 15:45
1
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267700%2fdifferent-round-behaviour-for-python-round-on-float-and-numpy-float64%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Calculating 435/100 in IEEE-754 basic 64-bit binary floating-point yields 4.3499999999999996447286321199499070644378662109375.
When this is rounded to the nearest decimal numeral with one digit after the decimal point, the result ought to be “4.3”. The Python rounding for this case appears to be correct.
For numpy.round
, the documentation refers to numpy.around
. The documentation for that says “Results may also be surprising due to … errors introduced when scaling by powers of ten.” Thus, it may be that numpy.round
does not calculate the correct conversion of 4.3499999999999996447286321199499070644378662109375 to decimal but rather performs a 64-bit binary floating-point multiplication of that by 10, which yields exactly 43.5 due to floating-point rounding, and then numpy.round
rounds that to 44 and formats it as “4.4”.
In summary, numpy.round
is not a correct rounding routine.
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’sprintf
andscanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.
– Eric Postpischil
Nov 13 '18 at 15:45
1
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
add a comment |
Calculating 435/100 in IEEE-754 basic 64-bit binary floating-point yields 4.3499999999999996447286321199499070644378662109375.
When this is rounded to the nearest decimal numeral with one digit after the decimal point, the result ought to be “4.3”. The Python rounding for this case appears to be correct.
For numpy.round
, the documentation refers to numpy.around
. The documentation for that says “Results may also be surprising due to … errors introduced when scaling by powers of ten.” Thus, it may be that numpy.round
does not calculate the correct conversion of 4.3499999999999996447286321199499070644378662109375 to decimal but rather performs a 64-bit binary floating-point multiplication of that by 10, which yields exactly 43.5 due to floating-point rounding, and then numpy.round
rounds that to 44 and formats it as “4.4”.
In summary, numpy.round
is not a correct rounding routine.
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’sprintf
andscanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.
– Eric Postpischil
Nov 13 '18 at 15:45
1
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
add a comment |
Calculating 435/100 in IEEE-754 basic 64-bit binary floating-point yields 4.3499999999999996447286321199499070644378662109375.
When this is rounded to the nearest decimal numeral with one digit after the decimal point, the result ought to be “4.3”. The Python rounding for this case appears to be correct.
For numpy.round
, the documentation refers to numpy.around
. The documentation for that says “Results may also be surprising due to … errors introduced when scaling by powers of ten.” Thus, it may be that numpy.round
does not calculate the correct conversion of 4.3499999999999996447286321199499070644378662109375 to decimal but rather performs a 64-bit binary floating-point multiplication of that by 10, which yields exactly 43.5 due to floating-point rounding, and then numpy.round
rounds that to 44 and formats it as “4.4”.
In summary, numpy.round
is not a correct rounding routine.
Calculating 435/100 in IEEE-754 basic 64-bit binary floating-point yields 4.3499999999999996447286321199499070644378662109375.
When this is rounded to the nearest decimal numeral with one digit after the decimal point, the result ought to be “4.3”. The Python rounding for this case appears to be correct.
For numpy.round
, the documentation refers to numpy.around
. The documentation for that says “Results may also be surprising due to … errors introduced when scaling by powers of ten.” Thus, it may be that numpy.round
does not calculate the correct conversion of 4.3499999999999996447286321199499070644378662109375 to decimal but rather performs a 64-bit binary floating-point multiplication of that by 10, which yields exactly 43.5 due to floating-point rounding, and then numpy.round
rounds that to 44 and formats it as “4.4”.
In summary, numpy.round
is not a correct rounding routine.
answered Nov 12 '18 at 19:19
Eric PostpischilEric Postpischil
78.3k883166
78.3k883166
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’sprintf
andscanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.
– Eric Postpischil
Nov 13 '18 at 15:45
1
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
add a comment |
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’sprintf
andscanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.
– Eric Postpischil
Nov 13 '18 at 15:45
1
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
It seems you are correct for numpy: As far as I understand the code it does a multiply by 10^x and then a round to int and divide: github.com/numpy/numpy/blob/… This would be the only possible implementation of round to decimals I can think of. Any idea what Python3 does?
– Flamefire
Nov 13 '18 at 7:28
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’s
printf
and scanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.– Eric Postpischil
Nov 13 '18 at 15:45
@Flamefire: Python is generally implemented by relying on the underlying platform. One can “easily” convert binary floating-point to exact decimal using techniques taught in elementary school, but straightforward implementations require more time and memory than is desired. Expert techniques to convert efficiently are known and published and are increasingly built into standard library functions such as C’s
printf
and scanf
. (Doing it efficiently is interesting and somewhat hard.) But I could not tell you from my own knowledge what any particular Python implementations do.– Eric Postpischil
Nov 13 '18 at 15:45
1
1
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
@Flamefire: On most platforms (those where the underlying floating-point format can be identified as IEEE 754), CPython versions 2.7 and 3.1 and later use dedicated correctly-rounded binary-to-decimal and decimal-to-binary conversions, based on well-known code by David Gay. The key source is here
– Mark Dickinson
Nov 13 '18 at 20:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267700%2fdifferent-round-behaviour-for-python-round-on-float-and-numpy-float64%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
They are both right,
4.35
is the same distance on the number line from4.4
as it is from4.3
. You could try allowing a higher number of decimals to kick the can a little farther down the road.– vencaslac
Nov 12 '18 at 18:05
Both implementations of
round
should round to even as explained in a comment to the linked question: "It's worth noting that this behaviour changed between Python 2 and Python 3; Python 3 behaves the same as NumPy here." I'm asking why this is not the case?– Flamefire
Nov 12 '18 at 18:16
1
@coldspeed This isn't a duplicate: the other question was about Python 2, this question uses a
print
function, so is using Python 3, where they should have the same behaviour.– Simon Byrne
Nov 12 '18 at 19:00
2
Python's
round
is correctly-rounded (but slow). NumPy's is not (but is faster). Different tradeoffs. (Agreed that this isn't a duplicate.)– Mark Dickinson
Nov 12 '18 at 19:01
@coldspeed: That question is not a duplicate. It says
numpy.round
rounds to even for values exactly halfway between rounded decimal values. But thenpVal
in this question cannot be exactly halfway between 4.3 and 4.4 because 4.35 is not representable in 64-bit binary floating-point. The closest representable value is 4.3499999999999996447286321199499070644378662109375, which ought to round down.– Eric Postpischil
Nov 12 '18 at 19:15