1. Should %d round?
- Posted by petelomax Mar 17, 2012
- 2141 views
Further to a recent post, I would like to solicit general opinion on whether
printf(1,"%d\n",3.9999999)
should print 3 (as it does now) or 4?
2. Re: Should %d round?
- Posted by DerekParnell (admin) Mar 17, 2012
- 2116 views
Further to a recent post, I would like to solicit general opinion on whether
printf(1,"%d\n",3.9999999)
should print 3 (as it does now) or 4?
I'm of the opinion that %d should display the integer portion of the number. If you need it rounded, do that explicitly.
printf(1,"%d\n",round(3.9999999))
However, I'll consider adding a rounding qualifier for the format() function, which is used by the formatting write routines. Something like ...
writefln("[r]",3.55) --> 4 writefln("[r10]",3.55) --> 3.6
3. Re: Should %d round?
- Posted by kenneth Mar 19, 2012
- 1968 views
We are hamstrung by Rob's early decision to follow Basic's example and assume that understanding numbers would be too difficult for his customers. Automatic numerical types will never do what user-specified types could accomplish. Choosing default behavior by voting will just reveal the shortcomings of democracy. Requiring user-specified numeric types should be a priority for Euphoria 5.
4. Re: Should %d round?
- Posted by SDPringle Mar 19, 2012
- 1962 views
What is Kenneth suggesting? Is it that we should have optional user-defined string cast operators?
-- allow double sized values but not the kinds with the fraction part... type hex(atom i) return floor(i) = i end type name_of hex(integer i) return sprintf("%x",i) end name_of hex x = 10 ? name_of(x) -- prints 'A', the hexadecimal form for ten.
5. Re: Should %d round?
- Posted by mattlewis (admin) Mar 19, 2012
- 1928 views
We are hamstrung by Rob's early decision to follow Basic's example and assume that understanding numbers would be too difficult for his customers. Automatic numerical types will never do what user-specified types could accomplish. Choosing default behavior by voting will just reveal the shortcomings of democracy. Requiring user-specified numeric types should be a priority for Euphoria 5.
I can guarantee you that they won't be, assuming you're talking about a C-like ability to specify the size or signedness of native euphoria variables. This would probably require a nearly full rewrite of most of euphoria.
Can you be more specific why you feel "hamstrung?"
Matt
IMHO IHBT
6. Re: Should %d round?
- Posted by Vinoba Mar 19, 2012
- 1890 views
Euphoria (or any other computer language) is only making real life easier, whether it is for calculating (math) or showing (video), presenting text (publishing), etc.
To show one digit always meant that you look at the second digit to decide, and to show five digits always meant that you look at the sixth digit before you decide.
It is absolutely ridiculous to say that somebody got 3 points if he really got 3.999 points in a contest. When you start using the word thousand or million it becomes even more ridiculous to say a city is 3 thousand miles away when it is 3.999 thousand miles away. It behoves us to look at the next (unwanted) digit and say that the city is 4 thousand miles away.
7. Re: Should %d round?
- Posted by mattlewis (admin) Mar 19, 2012
- 1902 views
Euphoria (or any other computer language) is only making real life easier, whether it is for calculating (math) or showing (video), presenting text (publishing), etc.
To show one digit always meant that you look at the second digit to decide, and to show five digits always meant that you look at the sixth digit before you decide.
Now you're just making things up! Consider the following C code (which is the basis for euphoria number formatting):
#include <stdio.h> int main(){ double d = 3.999; printf("%%d: %d %%1.0f %1.0f\n", (int) d, d ); return 0; }
$ gcc -o num num.c && ./num %d: 3 %1.0f 4
...and the equivalent euphoria code:
atom d = 3.99 printf(1, "%%d %d, %%1.0f %1.0f\n", { d, d })
$ eui num %d 3, %1.0f 4
It's the same!
It is absolutely ridiculous to say that somebody got 3 points if he really got 3.999 points in a contest. When you start using the word thousand or million it becomes even more ridiculous to say a city is 3 thousand miles away when it is 3.999 thousand miles away. It behoves us to look at the next (unwanted) digit and say that the city is 4 thousand miles away.
Yes, I would definitely agree with your point here. So, if you really want to round, then you should round. If you want to take the floor, you should take the floor. This is not an argument to change anything in euphoria, but in your code. Or perhaps an argument to change the way x86 and x86-64 work.
Matt
8. Re: Should %d round?
- Posted by ArthurCrump Mar 19, 2012
- 1894 views
I would recommend using "%d" only when the parameter is known to be an integer. Otherwise use "%.0f" to obtain a rounded result.
9. Re: Should %d round?
- Posted by gbonvehi Mar 19, 2012
- 1850 views
Further to a recent post, I would like to solicit general opinion on whether
printf(1,"%d\n",3.9999999)
should print 3 (as it does now) or 4?
I would leave it as it is and state clearly in the manual that the %d argument using an atom will print only the integer part.
If it needs to be rounded, round it :)
Cheers, Guillermo
10. Re: Should %d round?
- Posted by AJ_Oxley Mar 20, 2012
- 1795 views
Further to a recent post, I would like to solicit general opinion on whether
printf(1,"%d\n",3.9999999)
should print 3 (as it does now) or 4?
I would leave it as it is and state clearly in the manual that the %d argument using an atom will print only the integer part.
If it needs to be rounded, round it :)
Cheers, Guillermo
Sounds fine to me too :) But highlight it perhaps, or add it to the common (possible?) mistakes section.
11. Re: Should %d round?
- Posted by Vinoba Mar 20, 2012
- 1776 views
I would leave it as it is and state clearly in the manual that the %d argument using an atom will print only the integer part.
If it needs to be rounded, round it :)
Sounds fine to me too :) But highlight it perhaps, or add it to the common (possible?) mistakes section.
There is big pot-hole in the street where I live. It is surrounded by nice looking red pyramids, requiring everybody to drive round it. It has been there for about 3 month. I called the city, and they have acknowledged that it is a "mistake" that it has not been repaired. As I drive home at night I can't help praying that they have moved it out of the mistake section and now have it in the "pending work" section.
12. Re: Should %d round?
- Posted by jimcbrown (admin) Mar 20, 2012
- 1753 views
I would leave it as it is and state clearly in the manual that the %d argument using an atom will print only the integer part.
If it needs to be rounded, round it :)
Sounds fine to me too :) But highlight it perhaps, or add it to the common (possible?) mistakes section.
There is big pot-hole in the street where I live. It is surrounded by nice looking red pyramids, requiring everybody to drive round it. It has been there for about 3 month. I called the city, and they have acknowledged that it is a "mistake" that it has not been repaired. As I drive home at night I can't help praying that they have moved it out of the mistake section and now have it in the "pending work" section.
Then there's the guy who keeps trying to occupy city hall, demanding that all the traffic lights in the city be torn down so that the sky is clear for airplanes to fly through...
13. Re: Should %d round?
- Posted by Vinoba Mar 20, 2012
- 1774 views
I would leave it as it is and state clearly in the manual that the %d argument using an atom will print only the integer part.
If it needs to be rounded, round it :)
Sounds fine to me too :) But highlight it perhaps, or add it to the common (possible?) mistakes section.
There is big pot-hole in the street where I live. It is surrounded by nice looking red pyramids, requiring everybody to drive round it. It has been there for about 3 month. I called the city, and they have acknowledged that it is a "mistake" that it has not been repaired. As I drive home at night I can't help praying that they have moved it out of the mistake section and now have it in the "pending work" section.
Then there's the guy who keeps trying to occupy city hall, demanding that all the traffic lights in the city be torn down so that the sky is clear for airplanes to fly through...
The comparison or retort would only have relevance if the vast majority of citizens and experts felt that the traffic lights should be taken off AND if there also was a demand from the pilots to extinguish the city lights.
Your retort therefore, fails a logical examination.
In the case of the pot-hole I quoted, both the logical and the practical reasoning require the pot-hole to be repaired.
Q.E.D.
14. Re: Should %d round?
- Posted by jimcbrown (admin) Mar 20, 2012
- 1752 views
Euphoria (or any other computer language) is only making real life easier, whether it is for calculating (math) or showing (video), presenting text (publishing), etc.
To show one digit always meant that you look at the second digit to decide, and to show five digits always meant that you look at the sixth digit before you decide.
Now you're just making things up! Consider the following C code (which is the basis for euphoria number formatting):
#include <stdio.h> int main(){ double d = 3.999; printf("%%d: %d %%1.0f %1.0f\n", (int) d, d ); return 0; }
$ gcc -o num num.c && ./num %d: 3 %1.0f 4
...and the equivalent euphoria code:
atom d = 3.99 printf(1, "%%d %d, %%1.0f %1.0f\n", { d, d })
$ eui num %d 3, %1.0f 4
It's the same!
It is absolutely ridiculous to say that somebody got 3 points if he really got 3.999 points in a contest. When you start using the word thousand or million it becomes even more ridiculous to say a city is 3 thousand miles away when it is 3.999 thousand miles away. It behoves us to look at the next (unwanted) digit and say that the city is 4 thousand miles away.
Yes, I would definitely agree with your point here. So, if you really want to round, then you should round. If you want to take the floor, you should take the floor. This is not an argument to change anything in euphoria, but in your code. Or perhaps an argument to change the way x86 and x86-64 work.
Matt
One thing I'd like to add to this .. it isn't just Euphoria and C.
Python treats %d the same way:
>>> print "%d" % 1.99999 1
As does Perl:
$ echo 'printf("%d",1.99999)' | perl 1
As does gawk:
$ echo 1.999 | awk '{ printf "%d", 1.999 }' 1
As does Ruby:
$ echo 'printf("%d", 1.9999)' > test.rb $ jruby.sh test.rb 1
Bash seems like an exception, but it doesn't behave the way pro-rounding advocates want it to:
$ printf %d 1.9999 sh: line 0: printf: 1.9999: invalid number 0
Euphoria shouldn't just blindly do the same thing as "everyone else" does, but at the same time, when the majority of languages behave the same way, it may well be for a very good reason. Euphoria shouldn't blindly through away the lessons of the past, either.
15. Re: Should %d round?
- Posted by Vinoba Mar 20, 2012
- 1748 views
The logic:
In ASCII, ANSI, UNICODE, the characters 0-9 and the decimal point are characters.
Unlike most languages, Euphoria has chosen the route of recognizing literals and numeric characters as one, at least to start with and only later recognize the special characters as worthy of mathematical manipulations.
If we take "3.99999" as a string of literals only, we can safely say we want 1 character "3" or THREE characters "3.9".
However, the moment we recognize the decimal point as something special and meaningful mathematically, we are required to recognize the whole string as a number which needs to be interpreted and portrayed rationally in its full or abbreviated form.
Another argument for a more rational and practical approach is the fact of calculations occurring in hex and these are represented in decimal format.
It is conceivable that some calculations may end up as 3.99999 when actually the number might best be represented as 3.99998 or 4.00000. Therefore, in portraying the result of the previous hex calculations as 3.99999 is at best an approximation. Somebody might argue that it was 3.99999482397, but that it only reinforces my argument that very often the literal "3.99999" has a value not quite 3.99999
Hence the need for a rational mathematically sound and practical approach.
16. Re: Should %d round?
- Posted by jimcbrown (admin) Mar 20, 2012
- 1770 views
if the vast majority of citizens and experts felt that the traffic lights should be taken off AND if there also was a demand from the pilots to extinguish the city lights.
Then maybe in that case it would make sense to remove all the traffic lights. But that is obviously not true, and obviously it makes no sense to remove the traffic lights.
And the guy complaining is being nonsensical.
In the case of the pot-hole I quoted, both the logical and the practical reasoning require the pot-hole to be repaired.
I agree completely.
The comparison or retort would only have relevance...
Your retort therefore, fails a logical examination.
Q.E.D.
You seem to be saying that the comparison fails a logical examination because it fails to have relevance because it's obviously not true that we should remove all the traffic lights to make the sky clear for airplanes.
I am not arguing that the pot-hole should be repaired and the traffic lights should be removed and that %d should round.
I am arguing that the case that %d should round is more like the case of the traffic lights than the case of the pot-hole.
Therefore, I have not made the logic error of a non sequitur. My argument has relevance and passes a logical examination.
Let's go back to the bit of your post that I quoted:
if the vast majority of citizens and experts felt that the traffic lights should be taken off AND if there also was a demand from the pilots to extinguish the city lights.
Vast majority of citizens - or in this case users of programming languages - that feel that %d should round? I count two who have stated that they feel this way (maybe three if petelomax makes his views explicit), one of whom argued against the validity of using democracy.
Vast majority of experts who agree? I can't name a single one. Can you?
Demand from the pilots - or in this case the developers of the language? Not a single developer of Euphoria is in favor. We can take from the lesson of other languages such as Python, Perl, Ruby, C, and gawk that many developers of other languages are of the same mind as the Euphorian developers on this issue.
From this I conclude the case of %d rounding is more like the traffic light issue than the pot-hole issue.
17. Re: Should %d round?
- Posted by jimcbrown (admin) Mar 20, 2012
- 1781 views
The logic:
In ASCII, ANSI, UNICODE, the characters 0-9 and the decimal point are characters.
Agreed.
Unlike most languages, Euphoria has chosen the route of recognizing literals and numeric characters as one, at least to start with and only later recognize the special characters as worthy of mathematical manipulations.
I guess you mean that 32 can either be the decimal integer 32 or the ASCII character for a space.
I'm not sure that most languages are different. C, C++, Java are the same, whereas Basic and Python are different from Euphoria in this regard.
If we take "3.99999" as a string of literals only, we can safely say we want 1 character "3" or THREE characters "3.9".
However, the moment we recognize the decimal point as something special and meaningful mathematically, we are required to recognize the whole string as a number which needs to be interpreted and portrayed rationally in its full or abbreviated form.
This is not unique to Euphoria, but also applies to C, C++, Java, Basic, Python, Ruby, Perl, gawk, bash shell script, etc.
Another argument for a more rational and practical approach is the fact of calculations occurring in hex and these are represented in decimal format.
It is conceivable that some calculations may end up as 3.99999 when actually the number might best be represented as 3.99998 or 4.00000. Therefore, in portraying the result of the previous hex calculations as 3.99999 is at best an approximation. Somebody might argue that it was 3.99999482397, but that it only reinforces my argument that very often the literal "3.99999" has a value not quite 3.99999
Hence the need for a rational mathematically sound and practical approach.
I agree, but most of the time the language doesn't know which meaning is best. It tries to store the value as best it can in a floating point value (unless the programmer used a bignum representation to store values more accurately), but in the end the programmer is responsible for choosing how to display it.
The language can't guess. The programmer has to explicitly state how the value should be displayed. This includes what sort of rounding or flooring or ceiling to apply.
18. Re: Should %d round?
- Posted by AJ_Oxley Mar 20, 2012
- 1724 views
The language can't guess. The programmer has to explicitly state how the value should be displayed. This includes what sort of rounding or flooring or ceiling to apply.
IMHO, the problem could be catagorised as being either a hardware-derived inherent weakness(floating point rounding), or the failure of Euphoria to correctly guess what the programmer desires from "%d".
Even if there was a perfect solution to the hardware problem, we would still have the "assume" side. You know what they say about "assume" ;)
So a workable solution would be documenting the issue as a known gotcha, rather than making changes to Euphoria?
19. Re: Should %d round?
- Posted by mattlewis (admin) Mar 20, 2012
- 1711 views
The logic:
In ASCII, ANSI, UNICODE, the characters 0-9 and the decimal point are characters.
Unlike most languages, Euphoria has chosen the route of recognizing literals and numeric characters as one, at least to start with and only later recognize the special characters as worthy of mathematical manipulations.
If we take "3.99999" as a string of literals only, we can safely say we want 1 character "3" or THREE characters "3.9".
However, the moment we recognize the decimal point as something special and meaningful mathematically, we are required to recognize the whole string as a number which needs to be interpreted and portrayed rationally in its full or abbreviated form.
Your interpretation is one rational way to do things. However, it's not the way we do things, or the way most do things. You are making assumptions that are not valid in this case, and are certainly not required.
Another argument for a more rational and practical approach is the fact of calculations occurring in hex and these are represented in decimal format.
It is conceivable that some calculations may end up as 3.99999 when actually the number might best be represented as 3.99998 or 4.00000. Therefore, in portraying the result of the previous hex calculations as 3.99999 is at best an approximation. Somebody might argue that it was 3.99999482397, but that it only reinforces my argument that very often the literal "3.99999" has a value not quite 3.99999.
Yes, once again, you make a case for careful display of your results. No one has argued against this (although the calculations are done in binary, not "hex"). If anything, you reinforce the argument that you probably shouldn't be using %d for this sort of data.
Hence the need for a rational mathematically sound and practical approach.
How lucky for us that we already have one!
Matt
20. Re: Should %d round?
- Posted by DerekParnell (admin) Mar 20, 2012
- 1725 views
So a workable solution would be documenting the issue as a known gotcha, rather than making changes to Euphoria?
Yes, documenting this more accurately would be a good idea.
Also consider the following ...
atom x = 3.552 printf(1, "%d\n", x) -->3 printf(1, "%.0f\n", x) -->4 printf(1, "%.1f\n", x) -->3.6 printf(1, "%.2f\n", x) -->3.55 printf(1, "%.3f\n", x) -->3.552
The documentation needs to make clear that %d will only print the integer portion of a number. If one wants the decimals to influence the output you need to use the %f code instead, specifying the required rounding location.
21. Re: Should %d round?
- Posted by mattlewis (admin) Mar 20, 2012
- 1727 views
Also consider the following ...
atom x = 3.552 printf(1, "%d\n", x) -->3 printf(1, "%.0f\n", x) -->4 printf(1, "%.1f\n", x) -->3.6 printf(1, "%.2f\n", x) -->3.55 printf(1, "%.3f\n", x) -->3.552
The documentation needs to make clear that %d will only print the integer portion of a number. If one wants the decimals to influence the output you need to use the %f code instead, specifying the required rounding location.
Yes, I think those examples would be very good. I was considering adding language to the effect that using %d is equivalent to passing your value through floor(). At that point, I think the documentation will be pretty solid.
Matt
22. Re: Should %d round?
- Posted by petelomax Mar 21, 2012
- 1667 views
I would recommend using "%d" only when the parameter is known to be an integer. Otherwise use "%.0f" to obtain a rounded result.
Thank you everyone, esp the above suggestion.
Pete
23. Re: Should %d round?
- Posted by kenneth Mar 22, 2012
- 1657 views
"floor()" rounds down, while "%.0f" rounds toward zero. Be careful what you ask for. I still say a programming language that doesn't store numbers by explicit type shouldn't be used for arithmetic.
24. Re: Should %d round?
- Posted by mattlewis (admin) Mar 22, 2012
- 1573 views
"floor()" rounds down, while "%.0f" rounds toward zero. Be careful what you ask for.
Yeah, I was sorta wondering about what floor did, exactly, with negative numbers:
printf(1, "%d %.0f\n", -3.1 ) -- -4 -3 printf(1, "%d %.0f\n", -3.9 ) -- -4 -4
Your description of "%.0f" is incorrect. It seems to simply round as one would expect.
I still say a programming language that doesn't store numbers by explicit type shouldn't be used for arithmetic.
You're certainly welcome to that opinion, but it doesn't seem to make much sense to me. Euphoria does use explicit types, actually, and is pretty up front about them. Perhaps you were thinking about perl, or python, which can end up using its bignum if you overflow too much.
Matt
25. Re: Should %d round?
- Posted by FredRansom Mar 25, 2012
- 1486 views
I don't want it to round. But I would like truncation.