0

The following minimal example shows the issue:

#include <math.h>
#include <iostream>

int main()
{
   double a = log10(1/200);
   double b = log10(0.005);

   std::cout << "The value of a is " << a << " and b is " << b << std::endl;
}

I compile the program using g++:

g++ -o math math.cpp
./math

The output of the program is:

The value of a is -inf and b is -2.30103

The same thing occurs with C:

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

int main()
{
   double a = log10(1/200);
   double b = log10(0.005);

   printf("The value of a is %f and b is %f\n", a, b);
}

I compile the program using gcc:

gcc -o math math.c -lm
./math

The output is again:

The value of a is -inf and b is -2.301030

The answer in both cases should be -2.30103. Can someone explain to me what is going on?

Leigh K
  • 561
  • 6
  • 20

1 Answers1

5

1/200 is performing integer division, which is 0, so you're doing log10(0) which gives you -inf. Try changing that to log10(1.0/200.0) (or only one of them should need the decimal) to tell the compiler to do floating point division.

yano
  • 4,827
  • 2
  • 23
  • 35