0

Possible Duplicate:
Precision of Floating Point

I am trying to calculate the probability using some floating point numbers but always my final result is coming out as zero. Please find the below code and examples.

sd_attr4_ok = 3022.115234
unknwn_attr4 = 111
mean_attr4_ok = 32824.566406
var_attr4_ok = 9133180.000000

    (1/(sqrt(2*3.14)*sd_attr4_ok))*(1/pow(2.71828,((pow((atoi(unknwn_attr4)-mean_attr4_ok),2))/(2*var_attr4_ok))))

Please kindly help me in resolving this issue.

If I run the program below, I still get 0.000000:

#include <stdio.h>
#include <math.h>

int main()
{
        float a=(1/(sqrt(2*3.14)*3022.115234))*(1/pow(2.71828,(pow((111-32824.5666406),2))/(2*9133180)));
        printf("The probability is - %f\n",a);
        return 0;
}

And similarly, if I run the program below I still get 0.000000.

#include <stdio.h>
#include <math.h>

int main()
{
        float a=(1/(sqrt(2*3.14)*3022.115234));
        float b=pow((111-32824.5666406),2)/(2*9133180);
        float c=pow(2.71828,b);
        float d=1/c;
        printf("The probability is %f-%f-%f-%f\n",a,b,c,d);
        return 0;
}

My result-set:

***OK*** 0.908396-0.000084-0.000168-0.000000-0.000000
***FRAUD*** 0.091604-0.000835-0.000835-0.000000-0.000000
***OK FRAUD*** 0.000000 0.000000

If you see the above results the 4th and 5th attribute results is coming through the above program.And the third line represents the product of all the 5 values coming from OK and FRAUD. But my final results are coming out zero and not able to compare the values.

Community
  • 1
  • 1
Teja
  • 13,214
  • 36
  • 93
  • 155
  • you should use High-precision. Or you can use script language like ruby to do you compute. – laifjei Nov 24 '12 at 02:51
  • Don't use `%f`; use `%e` (or perhaps `%g`). – Jonathan Leffler Nov 24 '12 at 02:56
  • @laifjei: Changing the programming language doesn't give you any advantages in terms of numeric correctness or accuracy. – Dietrich Epp Nov 24 '12 at 03:05
  • Please use the advice you're being given! Your output values with the numbers mashed together with dashes separating them are unreadable. You've been advised that the trouble is that the values are out of range for `%f` which, by default, prints 6 digits after the decimal point. Some of your values are smaller than 1/1,000,000 so you see zero when you print them. There's no point in showing us that when 10 values all smaller than 1 and some smaller than 0.000001 are multiplied together, the result is less than 0.000001. – Jonathan Leffler Nov 24 '12 at 03:14
  • i would like to store the values rather than printing them. – Teja Nov 24 '12 at 03:34
  • @DietrichEpp ruby have high-precision support, eg, BigDecimal – laifjei Nov 25 '12 at 01:16
  • @laifjei: Yes, so does C: GMP. The main Ruby implementation is written in C. You are not going to win any language wars on Stack Overflow. In the immortal words of WOPR, "The only move winning is not to play." – Dietrich Epp Nov 25 '12 at 01:27
  • @DietrichEpp Yes, You are right, I just want to say that the problem is a precision problem, the type double in C cann't deal with. – laifjei Nov 25 '12 at 09:04
  • @laifjei: Except it's not a problem with precision. The problem is with the `%f` format specifier. Increasing the precision of your data type will not fix anything here. – Dietrich Epp Nov 25 '12 at 22:14

2 Answers2

2

Your term

pow(2.71828, ((pow...)))

comes out to a huge number (27806155998094886059376640.000000). The inverse of that number is too small for %f to show (with %e it is 3.596326e-26).

Try separating out the terms of your big expression and checking their values.

Edit:

Thanks for %e. But I need to compare two %e values now to get my desired results. Can you suggest me which data type I need to use to resolve this issue?

No you need to compare two double or float values. The %e is just the print format...

Sorry if this is too basic, but to compare the numbers use something like

float a = ...;
float b = ...;
if (a > b) {
    printf("%e\n", a);
} else {
    printf("%e\n", b);
}
William Morris
  • 3,554
  • 2
  • 23
  • 24
  • You're onto something — but 'too small for double' is not right. Too small to show with `%f` would be a better diagnosis; change `%f` to `%e` to see what you're getting. (`The probability is - 4.748625e-30`). – Jonathan Leffler Nov 24 '12 at 02:50
  • Thanks for %e. But I need to compare two %e values now to get my desired results.Can you suggest me which data type I need to use to resolve this issue? THanks. – Teja Nov 24 '12 at 02:55
  • `%e` is for output format. The `float` or `double` is fine as the data type. If you use `double`, you get more accuracy than using `float`, but since you've used a 3-digit approximation to π and a 6-digit approximation to _e_, accuracy is not at the top of the priority list. – Jonathan Leffler Nov 24 '12 at 03:01
  • Actually this one result is just a small piece.I am multiplying this value with 3 more floating point values and the final value is compared with another final value1 and the bigger one needs to be outputted. – Teja Nov 24 '12 at 03:03
  • @SOaddict: there's no problem. Do the extra calculations on this value; compare it with the other value. Just be prepared to deal with very small and very large values when formatting — use `%e` (or `%13.6e` for float values; `%23.16e` for double values) or `%g` instead of `%f` unless you know the values are suitable for fixed point formats. – Jonathan Leffler Nov 24 '12 at 03:08
1

You're using atoi() (which takes in a const char *) on what looks like an int. Are you not even getting a warning on that? What are you trying to do with atoi()?

1''
  • 26,823
  • 32
  • 143
  • 200