0

I was solving this problem on spoj http://www.spoj.com/problems/ATOMS/. I had to give the integral part of log(m / n) / log(k) as output. I had taken m, n, k as long long. When I was calculating it using long doubles, I was getting a wrong answer, but when I used float, it got accepted.

printf("%lld\n", (long long)(log(m / (long double)n) / log(k)));

This was giving a wrong answer but this:

printf("%lld\n", (long long)((float)log(m / (float)n) / (float)log(k)));

got accepted. So are there situations when float is better than double with respect to precision?

In78
  • 440
  • 4
  • 17
  • 5
    it's not that `float` is "better" than `double`. You should take rounding errors into account when converting a floating-point number to an integer. That's not as simple as casting it away. – The Paramagnetic Croissant Jun 11 '15 at 16:51
  • How do I safely convert a floating-point number to an integer? @The Paramagnetic Croissant – In78 Jun 11 '15 at 18:18

4 Answers4

2

A float is never more accurate than a double since the former must be a subset of the latter, by the C standard:

6.2.5/6: "The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double."

Note that the standard does not insist on a particular floating point representation although IEEE754 is particularly common.

rici
  • 234,347
  • 28
  • 237
  • 341
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

double will always give you more precision than a float. With double, you encode the number using 64 bits, while your using only 32 bits with float.

Edit: As Jens mentioned it may not be the case. double will give more precision only if the compiler is using IEEE-754. That's the case of GCC, Clang and MSVC. I haven't yet encountered a compiler which didn't use 32 bits for floats and 64 bits for doubles though...

Jouan
  • 166
  • 10
  • 4
    That's wrong. Doubles and floats may use the same number of bits (i.e. be indistinguishable) and they need not be a multiple of 32. Do not extrapolate from the one implementation you are familiar with :-) – Jens Jun 11 '15 at 16:56
  • That's true. It's only the case if the compiler is complying with IEEE-754. I have never encountered a compiler that didn't though. – Jouan Jun 11 '15 at 17:15
1

It might be better in some cases in terms of calculation time/space performance. One example that is just on the table in front of me - an ARM Cortex-M4F based microcontroller, having a hardware Floating Point Unit (FPU), capable of working with single-precision arithmetic, but not with double precision, which is giving an incredible boost to floating point calculations.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
1

Try this simple code :

#include<stdio.h>
int main(void)
{
    float i=3.3;
    if(i==3.3)
      printf("Equal\n");
    else
      printf("Not Equal\n");
    return 0;
}

Now try the same with double as a datatype of i.

Jens
  • 69,818
  • 15
  • 125
  • 179
s_bh
  • 162
  • 3
  • With all due respect...the code works well without the edit as well. – s_bh Jun 11 '15 at 17:25
  • 1
    You can kill mosquitoes with a 5kg hammer. Works well? Yes. Correct? No. – Eugene Sh. Jun 11 '15 at 17:41
  • Correct is relative. You have'nt specified the number of mosquitoes here. If there are enough mosquitoes lined up in a certain area and 5 Newton is the exact force required to kill all of them at one go in unit velocity per unit area, then its obviously correct. – s_bh Jun 11 '15 at 18:05
  • Ok, I see that metaphors don't work here. So in programming and not only, there are a good and bad practices. I is *wrong* to use the bad practices, and *correct* to use the good ones. Even if *accidentally* both are yielding the same correct result in a specific case. – Eugene Sh. Jun 11 '15 at 18:09
  • As for the code i added - it was optimally written.The additions were simply not required. – s_bh Jun 11 '15 at 18:10
  • `void main()` is simply wrong according to C standard. – Eugene Sh. Jun 11 '15 at 18:10
  • Thanks a lot for that tip.Will keep that in mind.PS:Its good that you gave up on the metaphors. – s_bh Jun 11 '15 at 18:13