abs() and fabs()
Re:abs() and fabs()
My guess about the name absolute is that it's used for distances. You have a distance between point X and Y, and the distance is X-Y or Y-X, both are correct. However, you have a direction in your distance. It's either positive (ahead) or negative (behind). If you just want to know how far it is you don't need a directional distance, but an absolute distance, and you make an absolute distance out of a directional one with the ABS function.
Re:abs() and fabs()
In mathematics, the absolute value (traditionally indicated by enclosing the value in parallel vertical bars) of a real number is magnitude without regard to sign - that is to say, how big it is. Thus, for real numbers, the absolute value of a positive number x is x, while the absolute value of a negative number x is -x - that is to say, a the same number, except positive. Absolute values are always postive for real numbers.
|10| = |-10| = 10
the abs() function is usually used where the sign of a number needs to be ignored; i.e., certain kinds of graphics routines involving mirror-image vectors.
|10| = |-10| = 10
the abs() function is usually used where the sign of a number needs to be ignored; i.e., certain kinds of graphics routines involving mirror-image vectors.
Re:abs() and fabs()
Correct.Perica wrote: Is fabs() meant to do the same thing as abs(), except on floating point numbers?
Depends on how your platform represents floating point numbers, and whether you really have a number or a NaN, infinite, or other "special" value.If this is the case, would this be the same thing as simply clearing the sign bit of an IEEE floating point number?
If you are into implementing a Standard C library, I strongly recommend P.J. Plauger's "The C Standard Library".
Every good solution is obvious once you've found it.