23

i wanted to convert double to float in C, but wanted to preserve the decimal point exactly as possible without any changes...

for example, let's say i have

   double d = 0.1108;
   double dd = 639728.170000;
   double ddd = 345.2345678

now correct me if i am wrong, i know that floating point precision is about 5 numbers after the dot. can i get those five numbers after the dot exactly as the double had it? so that above results as follows:

   float f = x(d);
   float ff = x(dd);
   float fff = x(ddd);

   printf("%f\n%f\n%f\n", f, ff, fff);

it should print

   0.1108
   639728.17000
   345.23456

all digits after the precision limit (which i assume as 5) would be truncated.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
sdfg
  • 393
  • 2
  • 5
  • 9
  • 9
    Floating point precision is more accurately stated as 'number of significant digits' rather than 'number of digits after the decimal place'. 12345678.12345 would most likely not be representable particularly accurately by a float. – Will A Aug 16 '10 at 23:25
  • 10
    Apart from the misapprehension regarding the representation of floating-point numbers that has been commented on and answered, the function `x` that you are looking for is the cast from double to float `(float)`. If you didn't change the rounding mode from its default of nearest-even, this cast computes the `float` that is the closest to the `double` you are converting. There is no better conversion than this one, so if this one loses too much precision for your taste, keep your `double`s as they are. – Pascal Cuoq Aug 16 '10 at 23:56
  • 2
    Truncation isn't the most accurate way to convert them - 345.2345678 is closer to 345.23457 than it is to 345.23456 – caf Aug 17 '10 at 00:00
  • What you want is impossible. `float` does not have that much precision (assuming it's IEEE single precision). Why do you want to use `float` instead of `double`? – R.. GitHub STOP HELPING ICE Aug 17 '10 at 06:16

3 Answers3

39

float and double don't store decimal places. They store binary places: float is (assuming IEEE 754) 24 significant bits (7.22 decimal digits) and double is 53 significant bits (15.95 significant digits).

Converting from double to float will give you the closest possible float, so rounding won't help you. Goining the other way may give you "noise" digits in the decimal representation.

#include <stdio.h>

int main(void) {
    double orig = 12345.67;
    float f = (float) orig;
    printf("%.17g\n", f); // prints 12345.669921875
    return 0;
}

To get a double approximation to the nice decimal value you intended, you can write something like:

double round_to_decimal(float f) {
    return round(f * pow(10, 7)) / pow(10, 7);
}
Finxx
  • 61
  • 1
  • 7
dan04
  • 87,747
  • 23
  • 163
  • 198
  • @davidvandebunte: That's only in C++. The question is tagged as C. – dan04 Dec 11 '17 at 22:08
  • 6
    Going through a string in `round_to_decimal` seems like a horrible idea. Surely there must be a much better way to do it with some math. – csl May 30 '18 at 09:02
13

A float generally has about 7 digits of precision, regardless of the position of the decimal point. So if you want 5 digits of precision after the decimal, you'll need to limit the range of the numbers to less than somewhere around +/-100.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
-3

Floating point numbers are represented in scientific notation as a number of only seven significant digits multiplied by a larger number that represents the place of the decimal place. More information about it on Wikipedia:

http://en.wikipedia.org/wiki/Floating_point

C.J.
  • 15,637
  • 9
  • 61
  • 77