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.