-2

I need to use the value 0.3. I am using float for this. Now when I move the mouse of this variable in VS2010, it tells me 0.2999999 instead of 0.3.

But I would really need the 0.3.

Can anybody tell me how this can be done? Thank you.

leppie
  • 115,091
  • 17
  • 196
  • 297
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 8
    You can't, 0.3 doesn't have an exact representation in base 2. See [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Mark Ransom May 05 '13 at 14:05
  • But in VB6 I can indeed assign a variable the value 0.3. And I need to produce the exact results in C++. – tmighty May 05 '13 at 14:06
  • Also see this one about the very same value - http://stackoverflow.com/questions/2909427/c-floating-point-precision . – Andy Thomas May 05 '13 at 14:08
  • 4
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) If you *really* need 0.3, you will need to use a different representation than floating-point types. @MarkRansom LOL oops, I didn't see you post that link tharr ;) – void ptr May 05 '13 at 14:08
  • http://en.wikipedia.org/wiki/IEEE_754-1985 – nico May 05 '13 at 14:10
  • @voidptr, no worries, I added it in an edit. – Mark Ransom May 05 '13 at 14:10
  • Added here since closed:Depending on the precision required, you could use integers and create a custom printing function. For two decimal points, a `1` would actually be a `100` and so on. Then you can use only integer math and automatically round errors. To print the numbers you could do something like this ( pseudocode ): string toString(int number) { std::string nString = standard_conversion_to_string(number/100); number = number % 100; if ( number != 0 ) { nString += "."; nString += covert_the_rest(number); } } – Svalorzen May 05 '13 at 14:14

1 Answers1

4

This depends on what you're trying to do. If you're trying to do something if the value is 0.3, you could try if (x > 0.29 && x < 0.31), although this won't be completely accurate. But other than this the first comment is right, there is no way to get the value accurately.

I would comment before posting to check what is being done, but I lack the reputation to do so. If this turns out to be inaccurate, I will happily delete this answer, so please don't be too quick to downvote.

EDIT: you could also try storing the number as ten times larger and comparing for 3, but I wouldn't recommend this. Please provide details of why this needs to be done.

RT_34
  • 300
  • 2
  • 12
  • +1. This is a good answer because you inferred the problem the user is facing and made a very good suggestion for how to solve it. Yes, in general it's appropriate to ask the use case in a comment before answering but I feel this was appropriate. – djechlin May 05 '13 at 14:12
  • 1
    ...but your edit is a worse idea. – djechlin May 05 '13 at 14:12
  • @djechlin Thanks very much for the upvote, I now have enough reputation to post comments in future examples – RT_34 May 05 '13 at 14:13
  • @djechlin just added something indicating that the second suggestion is a bad idea. Thanks for pointing that out – RT_34 May 05 '13 at 14:14
  • Why suggest it at all then? – djechlin May 05 '13 at 14:14
  • @djechlin He might feel that's a more appropriate way of doing it. While it might not seem like a good idea, it is still a possible solution, which the asker might prefer for whatever reason – RT_34 May 05 '13 at 14:16