+ 3

Why not Accurate???

in python, many calculations are not accurate. Why is so? For example: https://code.sololearn.com/cv5pz1sy3grG/

4th Feb 2019, 4:07 PM
3aͣlhͪaͣ_̈́
5 Answers
+ 8
Floating-point numbers are represented in computer hardware as base 2 (binary) fractions. https://docs.python.org/3/tutorial/floatingpoint.html
4th Feb 2019, 5:10 PM
Hubert Dudek
Hubert Dudek - avatar
+ 8
Fore more accuracy, use the decimal module. from decimal import Decimal, getcontext getcontext().prec = 5 print(Decimal(3.3) * Decimal(3)) # 9.9000
4th Feb 2019, 5:24 PM
Anna
Anna - avatar
+ 1
3aͣlhͪaͣ_̈́ Line 10 where you get the input, convert to float: i = float(input('')) Line 28 to print, you can specify precision with string formatting too: print(f'{i} x {n} = {i*n:.5f}')
4th Feb 2019, 7:55 PM
Tibor Santa
Tibor Santa - avatar
0
Ok that is a solution, but why does it shows slight difference?
4th Feb 2019, 5:59 PM
3aͣlhͪaͣ_̈́
0
https://code.sololearn.com/crr3SPxPeW2g/ For example if i program to make a table of some input.... input could be floating or int number...... what should be ammended there?
4th Feb 2019, 6:02 PM
3aͣlhͪaͣ_̈́