1

Can someone tell me how to solve this problem (in C++)? I try many ways to solve but I can't

This is my code

double n= 90.005;

cout<< setprecision(2)<< fixed<< n;

output: 90.00.

I expect output is 90.01.

But when I change n=90.006, output: 90.01

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    `90.005` is not representable exactly as a floating point value. So when you say you have a value of 90.005 that is not correct. Your value must be either slightly more or slightly less than 90.005. Since it is being rounded down it must be slightly less. There's not a lot you can do about this. – john Jan 13 '19 at 16:36
  • I know that. But my problem requires 90.01. It means you must turn 90.005 into 90.01 – Huy Nguyen Coder Jan 13 '19 at 17:03
  • If you need exact numbers don't use floating point, use fixed point instead there are many libraries that can implement this for you. – Alan Birtles Jan 13 '19 at 17:43
  • 1
    @HuyNguyenCoder Like I said there no such thing as 90.005. Until you accept that you aren't going to get very far. – john Jan 13 '19 at 19:50

0 Answers0