Python's integers (±0, 1, 2, etc.) have unlimited accuracy, which is unusual as far as programming languages go. In most languages they usually cap at 64-bits (aka long int) and there may be multiple integer types of various sizes. This makes Python very useful for scientific computing as you don't need a separate BigNum library to handle arbitrary precision. Python's floating-point numbers don't quite have this same luxury due to the inaccurate nature of them. Since many decimal numbers cannot be accurately represented in binary, floating-point math will always run into inaccuracies. For instance, 0.1 + 0.1 + 0.1 isn't 0.3, but something like 0.3000000000001. You'll use floats whenever whole numbers aren't enough, but you don't need a specific amount of accuracy. You probably never meant to ask about this, but decimal.Decimal is an alternative to float that lets you set its precision yourself. It's still not infinitely accurate, but it's often used in scientific computing where integers just can't cut it. Answer from Diapolo10 on reddit.com
Top answer
1 of 4
9
Integers (int type) are "whole numbers", without the fractional part, eg. 10. You use this type when the value is always an integer, e.g. a counter. Floating point numbers (float type) can have fractional part, e.g. 10.1234. They are stored using the closest representation in binary notation. Most numbers cannot be represented accurately, so they are slightly off. You use float for most mathematical calculations involving fractional numbers. Decimal numbers (Decimal type in Python) are used to represent floating point numbers accurately, with a defined precision (a defined number of places after decimal point). They are represented with two integer numbers, one for the integer part and one for the fractional part. For example. 10.1234 is stored as (10, 1234). Decimal type is required when fractional numbers must be represented accurately, with defined precision. The most notable example is financial calculations. Using float type you may get a result of a financial operation as 1010.123456$. But money is expressed with at most two decimal places. What does 0.123456$ mean? You can round it to 1010.12$, but then what happens with the remaining 0.003456$? Some "smart" programmers used that to their advantage in the past and they made a lot of money (which they eventually had to give back). So, for money calculations, you should use Decimal type. A good explanation of the Decimal type is in the documentation: https://docs.python.org/3/library/decimal.html
2 of 4
7
Python's integers (±0, 1, 2, etc.) have unlimited accuracy, which is unusual as far as programming languages go. In most languages they usually cap at 64-bits (aka long int) and there may be multiple integer types of various sizes. This makes Python very useful for scientific computing as you don't need a separate BigNum library to handle arbitrary precision. Python's floating-point numbers don't quite have this same luxury due to the inaccurate nature of them. Since many decimal numbers cannot be accurately represented in binary, floating-point math will always run into inaccuracies. For instance, 0.1 + 0.1 + 0.1 isn't 0.3, but something like 0.3000000000001. You'll use floats whenever whole numbers aren't enough, but you don't need a specific amount of accuracy. You probably never meant to ask about this, but decimal.Decimal is an alternative to float that lets you set its precision yourself. It's still not infinitely accurate, but it's often used in scientific computing where integers just can't cut it.
🌐
TestDriven.io
testdriven.io › tips › c1f6f393-5fa2-4edd-8b64-cc1344219173
Tips and Tricks - Decimal vs float in Python | TestDriven.io
You can use Decimal instead of float to: · incorporate a notion of significant places (1.20 + 1.30 = 2.50) represent decimal numbers exactly (1.1 + 2.2 = 3.3)
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
The exactness carries over into arithmetic. In decimal floating point, 0.1 + 0.1 + 0.1 - 0.3 is exactly equal to zero. In binary floating point, the result is 5.5511151231257827e-017.
Top answer
1 of 8
7

Java, 80 79 59 57 bytes

s->s.compareTo(new java.math.BigDecimal(new Float(s))+"")

Outputs a negative integer if the internal floating point value is larger; 0 if they're the same; and a positive integer if the floating point is smaller.

Try it online.

Explanation:

s->                          // Method with String parameter and integer return
  s.compareTo(               //  Compare the input to (resulting in neg/0/pos):
   new java.math.BigDecimal( //   Create a BigDecimal, with value:
    new Float(s))            //    The (32-bit) floating point number of the input
   +"")                      //   Convert that BigDecimal to a String

Minor note: I've used Float (which is 32 bits) and therefore holds slightly different values than in the challenge description. If I would change it to Double (which is 64 bits) the values would be the same as the challenge description. This difference can for certain inputs also result in different outputs (e.g. the "0.09" is 0.0900000035762786865234375 as float, resulting in -23, but 0.0899999999999999966693309261245303787291049957275390625 as double, resulting in 1). The overall functionality would still be the same, though:
Try it online.

2 of 8
5

Vyxal 3, 1 byte

1

Vyxal It Online!

Outputs 1 if equivalent, 0 if bigger, -1 if smaller. This probably polyglots a lot of languages lol. Leave a comment if you have a polyglot language where the answer is always 1.

Notably, this always outputs 1. That's because all numbers are stored exactly by default. That's an intentional language design decision we went out of our way to accommodate by using a third party library under the hood. Technically, the number type is called VNum which extends spire.Complex[Real], so not a "decimal" type but a type that happens to also be able to store decimals exactly by consequence of also storing things like surds exactly.

There's a slight chance this could be considered invalid by the "don't use decimal type" restriction, but that's up to the challenge asker as to whether the default (and only) generic number type being exact is allowed.

🌐
Reddit
reddit.com › r/learnpython › decimal point and the name "float"?
r/learnpython on Reddit: Decimal point and the name "Float"?
March 21, 2023 -

Hey, My maths sense are not working, can anyone explain how "a decimal point can appear at any position in a number"

Python calls any number with a decimal point a float. This term is used in most programming languages, and it refers to the fact that a decimal point can appear at any position in a number.

Edit:
3 / 2 = 1.5
Now how a decial point can appear at any position in 1.5?

Edit 2: is it like this? 0.15 * 10, 15 * 10**-1
Here, we are placing the decimal point any position in the number

🌐
LAAC Technology
laac.dev › blog › float-vs-decimal-python
Float vs Decimal in Python | LAAC Technology
March 4, 2021 - Generally, decimals exist in Python to solve the precision issues of floats.
Find elsewhere
🌐
Quora
quora.com › What-exactly-is-the-difference-between-decimals-floating-point-numbers-and-doubles-in-Python-programming-They-seem-to-get-addressed-interchangeably
What exactly is the difference between decimals, floating-point numbers and doubles in Python programming? They seem to get addressed interchangeably. - Quora
Answer (1 of 3): There is no such thing as doubles in Python itself, but Internally a floating point value is stored in a double precision C floating point value (as defined by IEEE 754 [1]) Floating point values are limited precision - they use a fixed number of bits to store a wide range of va...
🌐
The Teclado Blog
blog.teclado.com › decimal-vs-float-in-python
Decimal vs float in Python
October 26, 2022 - When working with decimal numbers in Python, we usually turn to floats. Floats serve us well for most purposes, such as simple divisions, but they do have limitations that can become incredibly problematic for certain use cases.
🌐
Reddit
reddit.com › r/learnpython › why do we need to differentiate between float numbers and integers?
r/learnpython on Reddit: Why do we need to differentiate between float numbers and integers?
August 13, 2022 -

Hi all. Im a complete beginner learning the basics. Im curious as to why Python has two different types of numbers (3 including complex) : Floats and integers. Why cant we just use any number and if we do wanna use a decimal point, we just use a decimal point without having to indicate it as a float? What is the significance of differentiating the two? Thanks!

Top answer
1 of 7
9
There are many reasons: It is faster for the computer to do math with integers than with floats. Floats accumulate errors, so if you don't need them, integers are more robust. For example 0.1+0.2 != 0.3. There are situations in which floats don't make sense but integers do. For example when accessing an item in a list, my_list[3] makes sense but my_list[3.57] does not. Every programming language (except JavaScript) also separates them. Many also have more than 1 type of int and float (for different sizes).
2 of 7
4
we just use a decimal point without having to indicate it as a float We can? print(type(1.5)) # There are at least 2 very good reasons to have separate types, though. The first is logical. Floating point math is inexact. For example: >>> (1 - 0.1) - 0.9 0.0 >>> (1 - 0.05 - 0.05) - 0.9 -1.1102230246251565e-16 As you can see, the more calculations with make with floating point arithmetic, the more likely it is we'll accumulate inaccuracies. This does not happen with integers, and we would certainly like to have the option of avoiding this problem whenever possible. It's not possible to eliminate this problem in rational numbers without giving a fractional number unlimited storage space, which is obviously undesirable in a variety of use cases. The second is practical. The algorithms for integer arithmetic are dramatically different. Integer arithmetic uses 2's complement, and floating point arithmetic uses the IEEE 754 standard for representing decimal numbers and performing computations on them. These different representations are what allows us to preserve precision in integers. Also, integer arithmetic is much faster than floating point arithmetic. When only integer arithmetic is needed over a large amount of data, computation will be significantly faster.
🌐
Reddit
reddit.com › r/learnpython › float and int function
r/learnpython on Reddit: Float and int function
March 25, 2023 -

Hello, please can someone explain to me when to use the float and int functions?

I mean should I use float when I am strictly dealing with decimal numbers, or when I want to convert whole numbers to decimal numbers?

pythonlearner

Top answer
1 of 4
5
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in °F (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians")
2 of 4
1
For string conversion, use the one appropriate to the string you're trying to convert. If you care about exact equality, don't use floats. Use integers, or maybe the Decimal class. If you want to know if floats are equal ish, keep in mind that the ish is always there. Use something like math.isclose to make such checks. There is rarely (almost never?) any need to explicitly convert integers to floats (unless you're doing something with numpy or similar). Integers will be automatically treated as floats during division (NOTE: different in some other languages), multiplication, etc. If you want to convert a float to integer, you can do that via int(2.3), but keep in mind how it rounds. There are other rounding functions that may or may not be more helpful (math.ceil, math.floor, round).
🌐
Jakeboxer
jakeboxer.com › blog › 2009 › 03 › 17 › benchmarking-python-decimal-vs-float
Benchmarking Python decimal vs. float - jBoxer
Just to make sure no overhead was getting in the way, I upped the limit to 40000 and ran it again. Decimal took 3.0 seconds, float 1.0. I can now confidently say that Python floats are about 3x the speed of Python decimals.
🌐
DEV Community
dev.to › kalebu › when-decimal-is-better-than-float-in-python-io6
When you should use Decimal over float in Python - DEV Community
May 12, 2021 - After a bit of googling, I came to realize that python does not store exact floating numbers but with tons of decimal other decimal precision(this has to do with the design of the language itself)
🌐
Reddit
reddit.com › r/learnpython › what is the importance of different float methods and functions/floating point numbers?
r/learnpython on Reddit: What is the importance of different float methods and functions/floating point numbers?
November 9, 2018 -

if python automatically prints numbers with decimals with their decimals what is the point of stuff like math.pow() or float() in general?

Basically, could some explain floating point numbers? I've done some googling and read in books but it didn't really make sense.

Thanks, python newbie here, really enjoying the learning process!

Top answer
1 of 1
2
what is the point of stuff like math.pow() math.pow is a bit of an oddity, because for many uses you could just as easily use the exponentiation (power) operator **. But generally, the functions in math are for mathematical functions that would be really hard to do on your own. How would you calculate trig functions like sin, cos and tan without the math module? could some explain floating point numbers? Numbers in a computer are stored used binary bits, zeroes and ones. Fixed point numbers use a fixed number of bits to represent a fractional number. Say you had an eight-bit fixed point number with three bits for the integer part and five bits for the fraction part. You can visualise how this would be stored in the computer's memory by drawing an empty box for each bit (in memory the box can't be empty, it must be either a 0 or a 1): □ □ □ □ □ □ □ □ Now colour the first three boxes red. They're used for storing the whole number part of your number. Colour the last five boxes yellow. They're the decimal part of your number. It's called "fixed point" because there is an implied decimal point between the two groups: □ □ □ . □ □ □ □ □ These fixed point numbers could store numbers between 0.0 and 7.96875 in steps of 0.03125. With floating point numbers, none of the boxes are fully dedicated to always being part of the whole number or part of the fractional number. The colours can change, and the implied decimal point "floats" from one side to the other as needed. With eight bits available, such a float could represent numbers as small as 0.00390625 and as big as 511.0 which is a big improvement over the fixed point number. Python floats have 64 bits in total: one bit to record whether it is positive or negative eleven bits for the exponent, which controls where the decimal point will go fifty-two bits for the "mantissa", the actual numeric digits which allows them to store numbers between 5÷10324 and 17976931348623157×10292 in both positive and negative, plus 0, positive and negative infinities, and a bunch of error codes called "NANs" (Not A Number). Now that I have explained this, you can forget all about it, because 98% of the time it doesn't matter in the slightest when programming in Python. Just treat floats as "numbers with a decimal point" and ignore the rest. But remember: if you want a whole number, use an int and don't type the decimal point; if you want a decimal like 0.5 or 1.5, use a float.
🌐
Codeforces
codeforces.com › blog › entry › 78645
Difference between Decimal and float in python - Codeforces
Difference between Decimal and float in python · By neo203, history, 5 years ago, For this task on Atcoder, from decimal import Decimal as dec a,b = map(dec,input().split()) print(int(a*b)) gives AC, but · a,b = map(float,input().split()) print(int(a*b)) gives WA.
🌐
Quora
quora.com › What-are-the-differences-between-float-and-decimal-data-types-in-Python
What are the differences between float and decimal data types in Python? - Quora
Answer (1 of 2): The underlying representation. One uses binary, the other one uses decimal. Both suffer from issues with loss of precision if you work with very large or very small numbers.
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.3 documentation
In the same way, no matter how many base 2 digits you’re willing to use, the decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base 2, 1/10 is the infinitely repeating fraction · 0.0001100110011001100110011001100110011001100110011... Stop at any finite number of bits, and you get an approximation. On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator as a power of two.
🌐
Medium
shiladityamajumder.medium.com › understanding-the-difference-between-float-and-decimal-in-python-18bae3b96ebc
Understanding the Difference Between float and decimal in Python | by Shiladitya Majumder | Medium
June 2, 2025 - The float type is a built-in data type in Python that implements binary floating-point arithmetic, adhering to the IEEE 754 standard. It is designed for speed and efficiency but has limitations when it comes to precision, especially with decimal fractions.
🌐
DevCamp
devcamp.com › trails › 28 › campsites › 214 › guides › decimal-vs-float-python
Decimal vs Float in Python
Even though decimal is inside a python we're going to have to explicitly call it because we can't simply use it the same way that we've used other functions leading up to this point. So before we get into anything let's come up with a few example variables. So I'm going to say product cost and let's set this equal to some type of floating-point number like 88.40 and I'm only keeping this zero here so it's easier to visualize it as a cost.