Originally posted by singularity2006
actually, are u familiar with scientific computations and how to find the correct number of significant figures? I’ve always wanted to figure out a way to produce a calculator that followed the correct logic. But it got really messy when I tried it in VB. What options do I have in C for that kind of dealy? This is only my third week in C so I don’t know much… just printf and scanf.
And that’s more or less all you’ll need.
Read ALL the options of printf and sprintf:
printf("%2.e", 0.0004);
Will already deal with printing in scientific notation.
One of the interesting things is that floating point numbers are stored in a form of scientific notation inside the computer (which also does tend to create other precision headaches, too).
The float type contains 32 bits: 1 for the sign, 8 for the exponent, and 23 for the mantissa. Its range is +/ 3.4E38 with at least 7 digits of precision.
The double type contains 64 bits: 1 for the sign, 11 for the exponent, and 52 for the mantissa. Its range is +/ 1.7E308 with at least 15 digits of precision.
The long double type contains 80 bits: 1 for the sign, 15 for the exponent, and 64 for the mantissa. Its range is +/ 1.2E4932 with at least 19 digits of precision. With the Microsoft C compiler, the representation of type long double is identical to type double.
Long double is also hardly ever used in any code, particularly these days.
ANSI C afaik does not have a built-in standard function to count decimal digits of floating point numbers. But one could be implemented by turning the value into a string (see sprintf or _gcvt, _fcvt if non-standard ansi) and then counting the number of characters past the ‘.’, disregarding trailing 0’s (if a period is available, that is). If you look around the web, I’m sure you will find some code to do this somewhere.
Note that current Intel processors many times do floating point operations in up to 80 bits and the result is later truncated. This is another headache to deal (in addition to the IEEE fp standard) with when you actually want lack of precision. But these problems are not inherent to C but to modern computers in general.
If you want better control of floating point, there are free libraries available that offer more precision and their own floating point types by trading some speed. But you likely don’t want to go there.