1

i wrote this code in c++. i expect 4932.08 for the answer(calculated by calculator)

#include <iostream>
#include <float.h>
#include <math.h>
    
using namespace std;
    
int main()
{
    long double a;
    a = LDBL_MAX;
    cout << "log10(" << a << ") = " << log10(a);    
}

sizeof(a) = 16 Bytes on my computer

instead of getting the correct answer, the output was inf. what is the problem? is it about the sizeof data log10 can get? what should i do to get correct answer?

I run this code with DEV and g++ (MinGW) by CMD. results are interesting.

DEV: inf

MinGW: 4932.08

sizeof(a):

dev: 16

MinGW: 12

why???

thank you

Saman
  • 11
  • 5
  • 1
    What does `log10l` do? What is `sizeof(long double)` on your platform? – Bathsheba Nov 26 '20 at 09:18
  • 1
    What is the *actual* output? I think I know the answer, but am not going to write it out until I know for sure. – Bathsheba Nov 26 '20 at 09:19
  • @Bathsheba What is unclear about `inf printed as the answer`? – eerorika Nov 26 '20 at 09:27
  • As far as I can tell, this is a bug in the standard library implementation. – eerorika Nov 26 '20 at 09:36
  • @eerorika: inf is not printed as the answer. Where is "log10" for example? I'm trying to establish if the issue is in the `log10` call (it probably is due to the incorrect headers - the long double overload does not have to be reachable via math.h), or in the ostream overload to long double. Really, I don't understand why you need me to state this - it ought to be obvious to you. – Bathsheba Nov 26 '20 at 09:38
  • Now your edit has ruined the question. If you're expecting 372.111571972 then long double and double are the same on your platform. – Bathsheba Nov 26 '20 at 09:40
  • @Bathsheba that number was what i expected. i calculated by calculator. – Saman Nov 26 '20 at 10:48
  • @Bathsheba as i said in last edit it is 16 bytes. sorry. first time in this site. i just learning c++ – Saman Nov 26 '20 at 10:50
  • 1
    @Bathsheba the actual output is *inf* – Saman Nov 26 '20 at 10:50
  • @eerorika really? is it a bug? – Saman Nov 26 '20 at 10:51
  • @Saman Correct result for logarithm of non-infinite input is not infinity. So yes, as far as I can tell, this is a bug. – eerorika Nov 26 '20 at 11:24
  • what compiler do you use? on gcc9.3.0 output of this code is _log10(1.18973e+4932) = 4932.08_ – Eugene Afanasyev Nov 26 '20 at 11:47
  • @EugeneAfanasyev you right, but the problem is variable sizeof. why in MinGW the size is different as DEV? – Saman Nov 26 '20 at 12:11

0 Answers0