3
#include<iostream>
using namespace std;

int main()
{
    unsigned int x = 293125555;

    double y =  (double)x/1000.0;

    cout << y << endl;

    return 0;
}

I expect the output to be 293125.555 but i get 293126 instead. Is this because of the way double are stored in memory?

balraj
  • 41
  • 1
  • The precision is set in cout , how do i get the precision in the variable itself? – balraj Nov 12 '19 at 12:22
  • 1
    @balraj There is no such thing as precision of variable. This is only limited by the type itself (e.g. `float` is less precise than `double`). `y` already stores `293125.555` (you can verify it using a debugger), but you print it with precision of 6 significant digits. – Yksisarvinen Nov 12 '19 at 12:24
  • 1
    @balraj The precision of the variable is only dependent on the variable type and your architecture and compiler. cppreference.com says about ```double```: "Usually IEEE-754 64 bit floating point type" – RL-S Nov 12 '19 at 12:25
  • 1
    In gdb, it says (gdb) p y $1 = 293125.55499999999 – balraj Nov 12 '19 at 12:25
  • 1
    @balraj [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – walnut Nov 12 '19 at 12:26
  • 2
    Good enough (a.k.a. as good as it can be with `double`). – Yksisarvinen Nov 12 '19 at 12:27

2 Answers2

3

std::numeric_limits<T> in limits:

Provides information about the properties of arithmetic types (either integral or floating-point) in the specific platform for which the library compiles.

std::setprecision in iomanip:

Sets the decimal precision to be used to format floating-point values on output operations.

So, you can use:

#include <iostream>
#include <iomanip>  // setprecision()
#include <limits>

using namespace std;

int main()
{
    unsigned int x = 293125555;
    double y = (double)x/1000.0;

    cout << setprecision(numeric_limits<double>::digits10 + 1)
         << y
         << endl;

    return 0;
}

and get the expected result:

293125.555
ne3suszr
  • 83
  • 7
1

Your problem is causing by cout. You should set the precision configuration for cout before using it as describer here

[precision:] Manages the precision (i.e. how many digits are generated) of floating point output performed by std::num_put::do_put.

You should modify your code like this to get the expected result:

cout.precision(3);
cout << y << endl;
Iman Kianrostami
  • 482
  • 3
  • 13
  • your quote is wrong. It is not about "floating point operations" but only output, correct quote is: "Manages the precision (i.e. how many digits are generated) of floating point output performed by std::num_put::do_put." – 463035818_is_not_an_ai Nov 12 '19 at 12:53
  • @formerlyknownas_463035818 I agree on it being about output, but since precision is a member function of `std::ios_base` as mentioned in quote, reasonable meaning of word **operation** in this context would be `Input/Output`. – Iman Kianrostami Nov 12 '19 at 12:59
  • actually, I find it more "wrong" that your post suggests that the quote is taken from the link you provide, but it is not. The thingy with "operation" meaning "output operation" is fine when reading the quote in the correct context, though it seems that OP is having problem with exactly that: telling apart the precision of the value and operations with it vs precision of the output – 463035818_is_not_an_ai Nov 12 '19 at 13:02
  • why do you keep saying it would be a "quote" ? Maybe you put the wrong link,the sentence is not a quote from the link you posted. Sorry for insiting on this, but paraphrasing and quoting literally are different things. – 463035818_is_not_an_ai Nov 12 '19 at 13:05
  • Check [here](https://en.cppreference.com/w/cpp/io/ios_base). I just put the link of that exact function. – Iman Kianrostami Nov 12 '19 at 13:06
  • @formerlyknownas_463035818 I have edited the answer just so you can make sure it is quote by just opening that specific link ;) – Iman Kianrostami Nov 12 '19 at 13:09
  • yeah had to reload, thats a quote now. Sorry for being difficult on this, but I think it matters – 463035818_is_not_an_ai Nov 12 '19 at 13:13
  • No problem. Thanks for the tip :) – Iman Kianrostami Nov 12 '19 at 13:18