Link to home
Avatar of jeffiepoo
jeffiepooFlag for United States of America

asked on

Convert a float variable to scientific notation

If I am working with very large float variables, and need to eventually display the output in scientific notation, how would I do that with printf?

I also do not want to lose anything from these numbers because of float limitations. Any recommendations?

Thanks guys.
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> I also do not want to lose anything from these numbers because of float limitations. Any recommendations?
Yes, don't use floats. If you need your numbers to be accurate represent them as fixed pointer number using integer types instead.
For example, rather than holding 1.234 as a float hold it as 1234 as an integer and perform all your calculations as integers.

It's probably worth reading this article, which gives a reasonable explanation of why floats are not accurate.
http://support.microsoft.com/kb/q145889/
This is also a very good read, explaining in some detail about how floats work in C++
http://www.codeguru.com/forum/printthread.php?t=323835
Avatar of jeffiepoo

ASKER

Well, unfortunately I NEED to keep them in float format.  Float variables can hold EXTREMELY large numbers if they are expressed in scientific notation, but the scientific notation part is the problem. How do I do this?
ASKER CERTIFIED SOLUTION
Avatar of LordOfPorts
LordOfPorts
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
That seems like it would work, thanks for the advice - however, I am kind of a newbie when it comes to c programming.

I understand all the lines in this code EXCEPT EVERY line that has a << or a >> in it. Could you elaborate on what these mean? Specifically these lines:?  Also, are these lines REQUIRED for the printf("%E", FLT_MAX) statements to work?  There is one more question at the bottom of the page

        cout << "FLT_MAX:\t\t\t" << FLT_MAX << endl;
        cout << "FLT_MAX scientific notation:\t"
        cout << endl;
        cout << "val:\t\t\t\t" << val2 << endl;
        cout << "val2 scientific notation:\t";
        cout << endl;


Would this work? - printf("%E", FLT_MAX*FLT_MIN)
The reason I ask is because I don't know if you can multiply 2 floats in scientific notation accurately without something going wrong.  

Thanks for all the help guys.

-Jeff
<< is a C++ ouput operator and is not needed if you use printf
floating point accuracy can be characterized by FLT_EPSILON DBL_EPSILON LDBL_EPSILON defined in
Which can give you an idea of how much accuracy might be lost when you multiply two floats
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
>> Well, unfortunately I NEED to keep them in float format
Ok, but then you cannot fulfill this requirement, " also do not want to lose anything from these numbers because of float limitations".

From the FAQ link I posted you...

Q: I have a problem with the following code.
 
Code:
 
int main()
{
  float a = 2.501f; 
  a *= 1.5134f; 
  if (a == 3.7850134) cout << "Expected value" << endl;
  else cout << "Unexpected value" << endl; 
}
Why does the program outputs "Unexpected value", because 2.501 * 1.5134 = 3.7850134?
 
A: Floating-point decimal values generally do not have an exact binary representation. This is a side effect of how the CPU represents floating point data...

Open in new window

SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.