Tuesday, May 19, 2020

Floats


When we consider the floating-point numbers,

Example:  2.5 , 0.4

In above example 2.5 looks normal when we write it in a program, although if our native language prefers to use a comma instead of a point in the number, we should ensure that the number doesn’t contain any commas at all.

Python will not accept that or may misunderstand the intentions, as the comma itself has its own reserved meaning in Python.

If we want to use just a value of two and a half, we should write it as shown above. We should carefully notice about this, there is a point between 2 and 5 but, not a comma.




But when writing zero point four can be written in Python, we can omit zero when it is the only digit in front of or after the decimal point.

So, we can write 0.4 as .4 and 4.0 can write as 4.

This will change neither its type nor its value.

Integers vs floats

If we take these two numbers 4 and 4.0


We may think that they are exactly the same, but Python sees them in a completely different way.
4 is an integer number and 4.0 is a floating-point number.

Also if we want to write very large numbers we can use scientific notation.

When we write the speed of light 3 x 108 , in Python it can achieved through the following way.

The letter E (or lower case letter e – it comes from the word exponent) is a concise record of the phrase times ten to the power of.

  1. The exponent (the value after the E) has to be an integer.
  2. The base (the value in front of the E) may be an integer.


Coding Floats

When we write 6.62607 x 10-34 in Python,




But sometimes Python may choose different notations.





Python always chooses the more economical form of the number’s presentation and we should take this into consideration when creating literals.

No comments:

Post a Comment