0

I've this issue: x_1 is an integer that has this value:

x_1: 463606075

what I want to obtain is 46.3606075, which the same number divided by 10000000. If I convert my int x_1 to a double, I obtain:

x_1: 463606075.000000

Now, if I divide by 10000000 (is 10^7), the last cipher are rounded: x_1: 46.360608, that is not 46.3606075 (the ...075 is rounded to ...08). I don't want any type of approssimation. How can I solve this problem? Thank you everybody for your help!

TryToLearn
  • 29
  • 1
  • 6
  • x_1 initially must be necessarily an integer. – TryToLearn Aug 19 '21 at 10:15
  • 1
    Reminder that floating point types can't represent every value exactly. – lulle2007200 Aug 19 '21 at 10:18
  • 3
    The result of converting `463606075` to a `double` (in IEEE-754 binary64 format) and dividing by 10000000 is exactly 46.3606075000000004138200893066823482513427734375. When you print this using `%f` or `%lf` with `printf`, “46.360608” is printed because the default precision for `%f` is six digits after the decimal point. To print the actual value, using `printf("%.99g\n", number);`. If you want an exact value, and 46.3606075000000004138200893066823482513427734375 is not good enough for you, you cannot use the `double` type; you must implement your own arithmetic. – Eric Postpischil Aug 19 '21 at 10:23
  • 1
    In the future, do not just describe what you have done in the program, by telling us yo converted a number and divided and got some result. Show the actual source code. Prepare a [mre]. It should show the starting numbers, the conversion, the division, and the code used to display the results. We should not have to guess what is in your program. – Eric Postpischil Aug 19 '21 at 10:26

2 Answers2

3

The last decimal place appears rounded, because you most likely only print the first 6 decimal places (which is the default precision for the %f format specifier of printf).

Also, don't forget that floating point types can't represent every value exactly (see this post for example). The exact value of your double would be 46.3606075000000004138200893066823482513427734375. You can easily test that for yourself with something like:

#include <stdio.h>

int main(void){
    int x = 463606075;
    double y = (double) x / 10000000.0;
    printf("%.100f", y);
}
lulle2007200
  • 888
  • 9
  • 20
  • thank you, but how can I obtain 46.3606075 from 46.3606075000000004138200893066823482513427734375? I'm not talking about the print, I need 46.3606075 value. – TryToLearn Aug 19 '21 at 10:33
  • 3
    You can't. That exact value is not representable (at least by a float/double). however, *46.3606075000000004138200893066823482513427734375* is what would be stored in your double variable, *independently of how you print it*. That precision should be good enough for most applications. – lulle2007200 Aug 19 '21 at 10:38
1

It was better to post whole of your code.

It seems you used printf or cout command to see the value of x_1. If you used printf("%f", x_1); then result must be 46.360608. If you want see more digits you can use like this:

printf ("%.7f", x_1);

Code Plus
  • 150
  • 1
  • 12
  • thank you, but how can I obtain 46.3606075 from 46.3606075000000004138200893066823482513427734375? I'm not talking about the print, I need 46.3606075 value. – TryToLearn Aug 19 '21 at 10:34
  • 1
    @TryToLearn: It is **not possible** to store the value 46.3606075 in the format commonly used for `double`. It may be possible to design a program to produce correct results that involve the number 46.3606075 in some calculations, but, to know that and answer questions about it, we would need to know more about the program and its goals. – Eric Postpischil Aug 19 '21 at 10:42