NO

Author Topic: fabs function  (Read 4945 times)

sannemander

  • Guest
fabs function
« on: April 26, 2012, 05:22:10 PM »
hello,

one question about the fabs function, I had to use it in this code to calculate a square root, but if I run the code without fabs the outcome is the same.
So I wonder why you have to use it.

Code: [Select]
#include <stdio.h>
#include <math.h>

int main(void){


double x,a=1,y=1;

printf("Enter a positive number: ");
scanf("%lf", &x);

do{

y= a;
a= fabs ((y + x/ y) / 2);}while(a != y);

printf("Square root: %lf\n", a);

return 0;

grtz!


CommonTater

  • Guest
Re: fabs function
« Reply #1 on: April 26, 2012, 07:29:03 PM »
Place your text cursor on the highlighted word fabs in your source code and press F1 on your keyboard for an explaination of what it does. 

You needed it incase you entered negative numbers.
 
 
« Last Edit: April 26, 2012, 07:33:30 PM by CommonTater »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: fabs function
« Reply #2 on: April 26, 2012, 07:43:06 PM »
Well, you don't have to use it as long as you only enter positive numbers!

And I am not sure that you algorithm is quite correct, what I have (probably from Bob Stout's C Snippets) is
Code: [Select]
y = x / 2;
  do
  {
                a = y;
                y = (a + x / a) / 2;
  } while ((a- y) != 0);
That also fixes the general problem with your comparison of two floating math values. Actually the "most correct" version would be
Code: [Select]
while ((a-y) > delta)with delta being an appropriately small floating point value...

Ralf

CommonTater

  • Guest
Re: fabs function
« Reply #3 on: April 26, 2012, 08:05:05 PM »
That also fixes the general problem with your comparison of two floating math values. Actually the "most correct" version would be
Code: [Select]
while ((a-y) > delta)with delta being an appropriately small floating point value...

Ralf

Good point Ralf... floating point comparisons are not fun since floating point math is only an approximation.... 
 
Maybe our friend should read THIS for some extra insight.
 
 

sannemander

  • Guest
Re: fabs function
« Reply #4 on: April 26, 2012, 08:19:41 PM »
hmmm the algorithm seems to work correctly, I knew there was a problem with comparing 2 floating type numbers,  that´s why they told me to use fabs. but I quess there´s more to it! thx for the tips!!! ;)

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: fabs function
« Reply #5 on: April 26, 2012, 08:36:36 PM »
hmmm the algorithm seems to work correctly, I knew there was a problem with comparing 2 floating type numbers,  that´s why they told me to use fabs. but I quess there´s more to it!
Certainly, as "using fabs" doesn't do anything in regards of using floating point numbers. Binary representation of binary number still is an issue, regardless if you use positive numbers or not (and that's all that fabs does!). Basically, it just flips the sign bit, but the bits of the coefficient still stay the same. And multiplications and divisions (as used in this sqrt algorithm) are among the "worst offenders" when it comes to mess with the binary representation....

Ralf

CommonTater

  • Guest
Re: fabs function
« Reply #6 on: April 26, 2012, 08:52:06 PM »
hmmm the algorithm seems to work correctly, I knew there was a problem with comparing 2 floating type numbers,  that´s why they told me to use fabs. but I quess there´s more to it! thx for the tips!!! ;)

One of the "unwritten rules" you will discover as you get more into this is that you never use floating point math for money.  This is because of a little nasty called "last bit ambiguity" wherein the last bit of a floating point calculation might or might not be 0.  Not all numbers can be accurately represented in the floating point format and sometimes rounding errors do occur... like $8.00 ending up as $7.999999999999 ... close enough for science, but an accumulating disaster for a bank.

FWIW... this is the vulnerability exploited in the movies Office Space and Superman II.

Doubles are more accurate than Floats but still have the problem.  Hense Ralf's warning.