Saturday, April 5, 2008

C Language Floating Point Questions - 15

Floating Point

1. When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
A: Most computers use base 2 for floating-point numbers as well as for integers. In base 2, one divided by ten is an infinitely- repeating fraction (0.0001100110011...), so fractions such as 3.1 (which look like they can be exactly represented in decimal) cannot be represented exactly in binary. Depending on how carefully your compiler's binary/decimal conversion routines (such as those used by printf) have been written, you may see discrepancies when numbers (especially low-precision floats) not exactly representable in base 2 are assigned or read in and then printed (i.e. converted from base 10 to base 2 and back again).

2. I'm trying to take some square roots, but I'm getting crazy numbers.Why ?
A: Make sure that you have #included , and correctly declared other functions returning double. (Another library function to be careful with is atof(), which is declared in )

3. I'm trying to do some simple trig, and I am #including , but I keep getting "undefined: sin" compilation errors.
A: Make sure you're actually linking with the math library. For instance, under Unix, you usually need to use the -lm option, at the *end* of the command line, when compiling/linking. 14.4: My floating-point calculations are acting strangely and giving me different answers on different machines.

If the problem isn't that simple, recall that digital computers usually use floating-point formats which provide a close but by no means exact simulation of real number arithmetic. Underflow, cumulative precision loss, and other anomalies are often troublesome.

Don't assume that floating-point results will be exact, and especially don't assume that floating-point values can be compared for equality. (Don't throw haphazard "fuzz factors" in, either; )

These problems are no worse for C than they are for any other computer language. Certain aspects of floating-point are usually defined as "however the processor does them" , otherwise a compiler for a machine without the "right" model would have to do prohibitively expensive emulations.

This article cannot begin to list the pitfalls associated with, and workarounds appropriate for, floating-point work. A good numerical programming text should cover the basics;

4. What's a good way to check for "close enough" floating-point equality?
A: Since the absolute accuracy of floating point values varies, by definition, with their magnitude, the best way of comparing two floating point values is to use an accuracy threshold which is relative to the magnitude of the numbers being compared. Rather than

double a, b;
...
if(a == b) /* WRONG */
use something like
#include
if(fabs(a - b) <= epsilon * fabs(a)) for some suitably-chosen degree of closeness epsilon (as long as a is nonzero!).
5. How do I round numbers?
A: The simplest and most straightforward way is with code like
(int)(x + 0.5)
This technique won't work properly for negative numbers, though (for which you could use something like (int)(x <>6. Why doesn't C have an exponentiation operator?
A: Because few processors have an exponentiation instruction. C has a pow() function, declared in , although explicit multiplication is usually better for small positive integral exponents.

7. The predefined constant M_PI seems to be missing from my machine's copy of
A: That constant (which is apparently supposed to be the value of pi, accurate to the machine's precision), is not standard. If you need pi, you'll have to define it yourself, or compute it with 4*atan(1.0).

8. How do you test for IEEE NaN and other special values?
A: Many systems with high-quality IEEE floating-point implementations provide facilities (e.g. predefined constants, and functions like isnan(), either as nonstandard extensions in or perhaps in or ) to deal with these values cleanly, and work is being done to formally standardize such facilities. A crude but usually effective test for NaN is exemplified by
#define isnan(x) ((x) != (x))
although non-IEEE-aware compilers may optimize the test away.
C9X will provide isnan(), fpclassify(), and several other
classification routines.

Another possibility is to to format the value in question using sprintf(): on many systems it generates strings like "NaN" and "Inf" which you could compare for in a pinch.

9. What's a good way to implement complex numbers in C?
A: It is straightforward to define a simple structure and some arithmetic functions to manipulate them. C9X will support complex as a standard type.

Free download online recent and current year solved placement question papers 2008 of leading companies in India and Abroad. See More Latest and previous expert, common, basic, important, advanced questions asked in college admission, entrance tests and technical interviews for 2007, 2008 companies like Infosys, wipro, satyam computers, hcl, ibm, tcs, cisco, etc. Recent, latest reasoning, multiple choice questions, verbal, non verbal, mathematics questions with solutions / solved / answer keys, booklet. Group Discussion GD guide with topics for leading campus placement interview, placement drive, HR Interview Guide with Questions and answers, how to behave, Tips, preparation, most recently asked / important software testing interview, free tutorials for java, oracle, c, c++, networking, web designing, windows 2000, vista, etc. questions. Keep Watching Previouspapers.blogspot.com for more stuff!

No comments: