Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » FormatDouble and numbers under 1e-15
FormatDouble and numbers under 1e-15 [message #25603] Wed, 03 March 2010 10:33 Go to next message
koldo is currently offline  koldo
Messages: 3398
Registered: August 2008
Senior Veteran
Hello all

There is a behavior I do not understand in function String FormatDouble(double d, int digits, int flags, int pad_exp) with numbers under 1e-15.

If you call FormatDouble(1e-16, 0) it returns "1e-16" instead of returning "0".

This is because of this line:
is_exp = ad && (ad <= 1e-15 || ad >= 1e15);


The question is, if I do not want decimals or a few of them, and number is very close to 0, the most intuitive output would be just 0, is not it ?


Best regards
Iñaki
Re: FormatDouble and numbers under 1e-15 [message #25624 is a reply to message #25603] Thu, 04 March 2010 10:16 Go to previous messageGo to next message
rylek is currently offline  rylek
Messages: 79
Registered: November 2005
Member
Hello!

Well, it depends. I wrote three formatting functions for doubles, FormatDoubleFix, FormatDoubleExp and FormatDouble. They are very loosely modelled after C printf formatting options %f, %e, and %g, respectively. Now as %g (FormatDouble) is supposed to automatically select between %f and %e, I employed the exponent check to use %f as a form of "optimization" or "user preferable" output for small absolute values of exponent. However whenever the exponent gets either too high or too low, formatting the number in fixed notation results in an unreasonably long string of zeroes. Keeping in mind the double's are floating points, for a number in the range, say, 1e-20, there are about 16 valid decimal digits 20 positions right of the decimal point, and to display such number as 0 is a complete loss of precision in my opinion. After all, printf("%g", 1e-20) also prints 1e-020 and not 0.

Regards

Tomas
Re: FormatDouble and numbers under 1e-15 [message #25625 is a reply to message #25624] Thu, 04 March 2010 10:50 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3398
Registered: August 2008
Senior Veteran
rylek wrote on Thu, 04 March 2010 10:16

Hello!

Well, it depends. I wrote three formatting functions for doubles, FormatDoubleFix, FormatDoubleExp and FormatDouble. They are very loosely modelled after C printf formatting options %f, %e, and %g, respectively. Now as %g (FormatDouble) is supposed to automatically select between %f and %e, I employed the exponent check to use %f as a form of "optimization" or "user preferable" output for small absolute values of exponent. However whenever the exponent gets either too high or too low, formatting the number in fixed notation results in an unreasonably long string of zeroes. Keeping in mind the double's are floating points, for a number in the range, say, 1e-20, there are about 16 valid decimal digits 20 positions right of the decimal point, and to display such number as 0 is a complete loss of precision in my opinion. After all, printf("%g", 1e-20) also prints 1e-020 and not 0.

Regards

Tomas


Hello Tomas

The comment comes from in FormatDouble() it is specified the number of digits, so if:

double num = 0.0000000000000001;
String str = FormatDouble(num, 0);

Now str = "1e-16".

As number of digits had to be 0, it seems the str would have to be just "0".


Best regards
Iñaki
Re: FormatDouble and numbers under 1e-15 [message #25639 is a reply to message #25625] Fri, 05 March 2010 10:43 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14105
Registered: November 2005
Ultimate Member
What about using FormatDoubleFix instead?
Re: FormatDouble and numbers under 1e-15 [message #25640 is a reply to message #25639] Fri, 05 March 2010 11:33 Go to previous message
koldo is currently offline  koldo
Messages: 3398
Registered: August 2008
Senior Veteran
luzr wrote on Fri, 05 March 2010 10:43

What about using FormatDoubleFix instead?

Yes, I will use it.


Best regards
Iñaki
Previous Topic: More Xmlize functions
Next Topic: Format(Time time, bool seconds) problem
Goto Forum:
  


Current Time: Fri Nov 01 00:10:42 CET 2024

Total time taken to generate the page: 0.02412 seconds