Reputation: 450
I have a self made stack and I'm writing a method push() for it. I am pushing one number to the stack, sometimes it's int and sometimes it's double. When it's double, I've got another variable saying what precision it should have (how many numbers after coma).
Let's say I've got variables d = 7,39518 and prec = 3, can I somehow set precision of d to value of prec so the outcome is that value of d = 7,395?
I know how to do it for couting, but how to change the actual variable?
Upvotes: 3
Views: 185
Reputation: 145204
Neither the core language nor the standard library offers such a type.
In other languages and libraries it's known as a decimal type, and more in general, e.g. in computer science, as a fixed point type. E.g. C# has an 128-bit decimal
type, and the Windows API offers a 64-bit DECIMAL
type (part of the OLE Automation set of types). Essentially a value consists of a signed integer for the digits, and either a small integer specifying the number of decimals, or knowledge of such a value specified at compile time.
You can easily implement such a type yourself, but the Boost library offers cpp_dec_float
.
For more general info about floating point, google up what every scientist should know about floating point.
Upvotes: 3