How to use decimal numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use decimal numbers

I need to use decimals but the code doesn't accept them

11th May 2019, 6:12 PM
Manuel Dall'Olio
Manuel Dall'Olio - avatar
6 Answers
+ 2
Attention! Float/Double is not the same. Fix point numbers (like Decimal) can represent every number in their decimal places, floating points (float, double) can't. Example: 0.2 is not representable with float! Experimental prove: try to add 1.000 times 0.2 and print out the result. Manuel Dall'Olio Standard C++ std::decimal::decimal32, std::decimal::decimal64, and std::decimal::decimal128 Of course you can use others (like BOOST), or write your own datatype.
11th May 2019, 7:05 PM
Daniel Adam
Daniel Adam - avatar
+ 1
#include <decimal> using namespace std; using namespace std::decimal; int main() { decimal32 d = 4.7df; // your code }
11th May 2019, 7:54 PM
Daniel Adam
Daniel Adam - avatar
0
C++
11th May 2019, 6:17 PM
Manuel Dall'Olio
Manuel Dall'Olio - avatar
0
Well no, I hadn't even thought about it. I will try that
11th May 2019, 6:20 PM
Manuel Dall'Olio
Manuel Dall'Olio - avatar
0
I am a beginner so, please, be patient. How can I incorporate std::decimal::decimal 32 into my code?
11th May 2019, 7:18 PM
Manuel Dall'Olio
Manuel Dall'Olio - avatar
0
Aaaah ok thanks
11th May 2019, 7:58 PM
Manuel Dall'Olio
Manuel Dall'Olio - avatar