0

I have a Client program by tcp connection that send data to a server. In client I need send a normalized decimal number to server for normalization I Multiplier decimal number to 100,000 then send it to server but I get wrong number in server. for example.

double price;

I set it from Gui to 74.40

cout<<price; ---> 74.40

and when I serial my object I send

#define Normal 100000
int tmp = price*Normal;
oDest<<tmp;

In wireshrk I see that client sent 7439999.

why this happened? how I can pervent this problem?

herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49
  • 3
    See [here](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) and [here](http://floating-point-gui.de/) – sbi Jun 26 '12 at 15:32
  • You should probably look at [this](http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems) – Dan F Jun 26 '12 at 15:33
  • @sbi, You should see [Why your links should never say click here](http://uxmovement.com/content/why-your-links-should-never-say-click-here/). – Mark Ransom Jun 26 '12 at 15:38
  • "You should probably look at this" falls in the same category as "click here". – David Hammen Jun 26 '12 at 15:47
  • @MarkRansom: I know that reasoning, but I disagree with it for this case. First, every browser worth downloading will show these links before you need to click on them. Second, I don't consider it necessary to meticulously adhere to UX standards for a quick, one-off comment that points a programmer at some web resource. (Should I prepare a press release again, I will try to think about all those rules, though.) – sbi Jun 26 '12 at 15:48
  • @sbi, I only mention it because I hate seeing links with no idea of what they're about. I was able to guess pretty easily, but I'm sure that's not the case for everybody seeing this question. The article is certainly overkill in the context of comments but I think it makes some pretty good points. – Mark Ransom Jun 26 '12 at 16:01
  • @MarkRansom: Those where simply the first two interesting links google spit at me for "what every programmer should know". I did remember and searched for the Goldberg article, but I threw in the second link just because it seems to be an easier read. – sbi Jun 26 '12 at 16:08

3 Answers3

1

Don't store anything as a floating point value. Use a rational number instead, or use a fixed point value. Floating point values (like double) basically "cheat" in order to fix a large range of possible values into a reasonable chunk of memory, and they have to make compromises in order to do so.

If you are storing a financial value, consider storing pennies or cents or whatever is the smallest denomination.

Rook
  • 5,734
  • 3
  • 34
  • 43
  • thanks but ,how I can Impelement a `fixed point value` should I build new object? – herzl shemuelian Jun 26 '12 at 15:48
  • @herzlshemuelian: That's a bit outside of the scope of an easy answer, however the internet does have plenty of suggestions. There are two SO questions with a few useful links available, ["best way to do fixed point math"](http://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math) and ["c fixed point library"](http://stackoverflow.com/questions/2945747/c-fixed-point-library) which may point you in a useful direction. But what are the values actually supposed to represent? – Rook Jun 26 '12 at 15:59
  • I need math function like Multiplier and scan it from string and print to a stream as string or normalizaed number (int). – herzl shemuelian Jun 26 '12 at 16:23
  • @herzlshemuelian: I appreciate that, but what do that values actually represent? Are they financial, for example? Were any of the links helpful? – Rook Jun 26 '12 at 16:37
0

This is due to floating point precision errors. You can add some rounding:

int tmp = (price + 0.5/Normal)*Normal;
Gustav Larsson
  • 8,199
  • 3
  • 31
  • 51
0

You need to round the number as you convert it to integer, due to the inability of floating point to represent a decimal number exactly.

int tmp = price*Normal + 0.5;
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622