- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Why are floating-point calculations inaccurate in Python?

## What is a float?

The floating-point are also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

## Why floating-point calculations inaccurate?

The floating-point calculations are inaccurate because mainly the rationals are approximating that cannot be represented finitely in base 2 and in general they are approximating numbers which may not be representable in finitely many digits in any base.

## Example

Let’s say we have a fraction −

5/3

We can write the above in base 10 as −

1.666... 1.666 1.667

As shown in the above representations, we associate and consider both i.e., 1.666 and 1.667 with the fraction 5/3, even though the first representation 1.666… is actually mathematically equal to the fraction.

The second and third representations 1.666 and 1.667 is having an error on the order of 0.001, which is more problematic than the error between let’s say, 9.2 and 9.1999999999999993. The second representation isn't even rounded correctly!

As we don't have an issue with 0.666 as a representation of the number 2/3, therefore we shouldn't really have a problem with how 9.2 is approximated.

**Note** − There are infinitely many real numbers and there are infinitely many rational numbers. The floating-point representation is a finite one, therefore inevitably a lot numbers are impossible to represent. Precisely, 64 bits only allow you to distinguish among only 18,446,744,073,709,551,616 different values.

Let us see some conversions and its results. This will hep us in understanding more about floatingpoint number system.

## Convert floating to binary (base 2 number system)

We will see how to convert floating-point value to binary. Binary uses two digits, 0 and 1. Also called as base 2 number system Each position in a binary number represents a 0 power of the base (2). Last position in a binary number represents a x power of the base (2).

First, we take the integer part from the floating-point value and convert it to binary then take fractional part and convert it to binary form and lastly combining both −

### Example

def floatoctal_convert(my_number, places = 3): my_whole, my_dec = str(my_number).split(".") my_whole = int(my_whole) my_dec = int (my_dec) res = bin(my_whole).lstrip("0b") + "." for x in range(places): my_whole, my_dec = str((my_decimal_converter(my_dec)) * 8).split(".") my_dec = int(my_dec) res += my_whole return res def my_decimal_converter(num): while num > 1: num /= 10 return num # Driver Code n = input("Enter floating point value : \n") p = int(input("Enter the number of decimal places of the result : \n")) print(floatoctal_convert(n, places = p))

### Output

Enter floating point value :2.34 Enter the number of decimal places of the result :3 10.256

## Convert floating to octal (base 8 number system)

Octal Number uses eight digits, 0,1,2,3,4,5,6,7. Also called as base 8 number system. Each position in an octal number represents a 0 power of the base (8). Last position in an octal number represents a x power of the base (8).

Decimal number system has base 10 as it uses 10 digits from 0 to 9. In decimal number system, the successive positions to the left of the decimal point represent units, tens, hundreds, thousands, and so on.

### Example

Given a float decimal value and input the decimal places number, our task is to convert it to octal form −

def float_convert_octal(my_number, places = 3): my_whole, my_dec = str(my_number).split(".") my_whole = int(my_whole) my_dec = int (my_dec) res = oct(my_whole).lstrip("0o") + "." for x in range(places): my_whole, my_dec = str((decimal_converter(my_dec)) * 8).split(".") my_dec = int(my_dec) res += my_whole return res def decimal_converter(num): while num > 1: num /= 10 return num n = input("Enter the floating point value : \n") p = int(input("Enter the number of decimal places of the result : \n")) print(float_convert_octal(n, places = p))

### Output

Enter the floating point value : 6.89 Enter the number of decimal places of the result : 12 6.707534121727