Re: Should %d round?

new topic     » goto parent     » topic index » view thread      » older message » newer message
mattlewis said...
Vinoba said...

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!

Vinoba said...

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.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu