Setting a fixed decimal place precision for float variable, C++

xor_coding
xor_coding used Ask the Experts™
on
How do I set a fixed (specified) number of decimal place precision for float variable. I am writing a program with frequent irrational number, and I want a way by which the float variable will store all the variable value at a fixed decimal place value.

So for example

//speicify a mechanism to store all float variable to 3 decimal place.

float time1 = 1/3;
float time2 = 2/3;
cout << "The values are stored as " << time1 << " and " << time2;

OUTPUT
The values are stored as 0.333 and 0.667

And when I say 0.333, I really mean 0.333 only, i.e. 0.33300000....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
Can you store time*1000 as integers?

Author

Commented:
why would I need to store time*1000 as integer?

I can see your point anyway, but this way I would have to do this multiplication "every time," which doesnt sound like a very feasible approach.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Every time you do what?   What do you do with the number and how many times do you do it?

If you store floating point numbers, you won't get exactly 0.33300000....
depending on the format, it may actually be something like
0.333000000000000018207657603852567262947559356689453125
Which C++ compiler are you using?

The fundamental problem is that decimal (base 10) fractions often have no precise equivalent when stored in a binary representation, as ozo's example illustrates. That's why representations such as binary-coded decimal (BCD) are often used for monetary calculations. They can lose some storage capacity and some efficiency but can gain some precision.

Generally, if you need fixed fractional decimal positions, don't use floating point. Use a decimal data type.

Alternatively, convert the decimal fraction to integer and perform the conversion for presentation. So, for example, if three decimal positions are needed, multiply by 1000 to store the integer value and divide the integer by 1000 when presenting the value. By converting to integers, some of the problems of working with decimal fractions in a binary mode can be eliminated.

Tom
Understand that floating point numbers are sort of the reverse of integers.  (The following isn't an exact explination, but it explains the concept)  

With an integer, the 1 bit represents 0 or 1, the 2nd bit represents 0 or 2, the 3rd bit represents 0 or 4, etc.  So to get a value of 5, you must add 4 + 1 (in bits that 101).

With floating point numbers, values less than 1 are represents by fraction bits.  So bit -1 represents 0 or 1/2, bit -2 represents 0 or 1/4, bit -3 represents 0 or 1/8.  So to represent a value like 0.75, the bits are (110) (that's 1/2 + 1/4 + 0/8).  Now the computer only has a limited number of fraction bits, so it's not possible to represent ever possible number.  For example, there is no combintion of 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, etc that will ever equal exactly 0.333.  The best you will get is a combination of fractions that give you 0.333000000000124 (or something like that).

If you MUST keep up with an exact decimal value to a certain number of places, your only option is to use integers with an implied decimal point.  So to maintain 3 points of accuracy, you logically multiply everything by 1000.  That way 1000 means 1, while 333 means 0.333.  And yes, every time you read a value and go to store it, you must multiply by 1000, and when you go to display a value, you have to logically divide by 1000.  Not only that, you're also limited in the largest number you can represent.  So if you are using 32 bit integers, normally you can have values from ABOUT  -2 billion to +2 billion.  But if you mulitply everything by 1000 and use 32 bit integers, you're limited from ABOUT -2 million to +2 million.  However, putting everything in "fixed point" integers means your math will run faster.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial