Doubt Python, why 15.0? (I´m a begginer, it´s not spam or anything, i just want to know)


#1

Hi guys! I´m just getting started with python and I noticed it gives error if i just divide 15/100 and I have to put 15.0/100, can someone this to me plz?


#2

The older courses use Python 2 which return an integer for integer division. Therefore,

15 / 100 

will be 0. To cure this, use the explicit float() function…

float(15) / 100

Newer Python courses will likely use Python 3 which automatically returns a float for all division.


#3

Python 2 uses integer division. It does not automatically cast the result to floating point.

Why?

In Python 2, when dividing 2 integers (whole numbers), it returns the floor and not the floating point number. Huh?

What is the “floor”? It’s the largest integer value that’s >= 15 / 100.

15 / 100 = 0.15 and therefore, it returns a floor of 0.

Why 15.0 / 100

In Python 2, you can either convert it using float(15) or simply type it as a floating point number, which you did.

Python 3 takes care of it for you, as it returns the true value, which would be in floating point.


#4

Thanks @mtf and @hellofromtonya , if you can explain me too: what is a float?, and from that topic I understand the floor just considers numbers withouth decimals?


#5

A float` is a computer term that is short for floating point number.

Any number less than zero but greater than -1, or greater than zero but less than 1 is a float, which is just a technical term describing decimal fractions.

The reason we call it a computer term is because we don’t normally use the term in math, unless it is floating point arithmetic, a complex form or arithmetic done with binary numbers.

Binary numbers consist of only 1’s and 0’s. Similar to how we call base 10 numbers, decimal, we call base 2 numbers, binary. deci is a prefix that signifies, 10. bi is a prefix that signifies 2.

There are no decimal fractions (technically, Real Numbers) in binary arithmetic. There is no way for you or I to wrap our heads around the complex process involved that allows a computer to convert Real numbers to binary near equivalents. Your computer has a special chip called the MPU in the CPU that is dedicated to this process.


#6

oh, I see, thank you for the explaning mate @mtf ^^


#7

A float() converts the integer into a floating point number. A very elementary explanation is: it’s a number that has a decimal point. Why? To increase the precision (accuracy) of the value and result.


#8

So, here comes the next question… : if I use float() it will define the decimals the answer will have or will it just let the 15/100 be correct and the answer would be 0.15, meaning that if I write it with float() it will give a full answer or will it just define the number of decimals my variable can have? And by the way, I´ve read some of your page Tonya, seems interesting. I´ll read some more later, I want to read ´´my story´´


#9

Not unless it is properly applied.

floar(15 / 100)

will be, 0.0 since the integer division takes place first.

float(15) / 100

tells Python to treat the numerator as a float so that the result is a float. The above will give, 0.15.

The float() function does not pay attention to number of decimal places. That is where the round() function comes in.

>>> from math import pi
>>> round(pi, 3)
3.142
>>> 

#10

oh, ok, thanks a lot. This page is awesome and has very fast answers from users, thank you a lot! both ^^


#11

You’re welcome. Hope these concepts are beginning to gel in your mind.

A little more number discussion would be good, right about now while we’re on the subject.

Consider the terms, floor, ceiling, and, round. From above,

>>> p = round(pi, 3)
>>> type(p)
<class 'float'>
>>> 

We see that the round function will return a float when given a float, and when given an integer,

>>> type(round(3))
<class 'int'>
>>> 

Of course it makes no sense to pass an integer to this function since it does nothing but waste CPU clicks.

Still working with floats, let’s explore floor and ceiling.

>>> from math import floor, ceil
>>> floor(8 / 13)
0
>>> ceil(8 / 13)
1
>>> type(floor(8 / 13))
<class 'int'>
>>> type(ceil(8 / 13))
<class 'int'>
>>> 

We don’t need to import any math classes to get a floor value, which is always rounded down to the nearest integer. For that we have, int()

>>> int(pi)
3
>>>  int(8 / 13)
0
>>>

It follows that we can force the ceiling by adding 1 to the above.

>>> int(8 / 13) + 1
1
>>> 

Python has a built in arithmetic operator for floor division which yields a floor from a quotient.

123 // 10  =>  12
12 // 10   =>  1

Much of what we’ve written above will be touched upon as you progress but extra reading is assumed while you go through the concepts. Don’t worry if this is confusing, just now. It will sort itself out in due course of time.


#12

I would argue that precision is not synonymous with accuracy.

Precision concerns itself with Real number computations and how many significant figures are used in the calculation, and appear in the final result. We also refer to them as sig-digs for significant digits.

Accuracy concerns itself with measurement, which is something we cannot arbitrarily force. Our measurements are only as accurate as the tool used to perform them.

Not everyone’s foot is 1 foot long, but we commonly pace out using our feet to approximate a given distance. Measurements in physics such as wave length or nuclear radii require much more sensitive instruments. Millrights use measurements in the thousandth of an inch. That’s a billion times less accurate than the measure of nuclear radii.

Consider e and pi and a host of other transcendental numbers. In our calculations we generally let the computer or calculator provide the range of precision on these numbers, since they can have any number of decimal places. How many are significant to our intended use come down to how many decimal places are in our supplied values, however acquired. The more precise our provided values, the more precision we can aim at in our computed result.

Say we start with the measurement 7.689 mm. As millimeters go, this is a very precise value. How accurate it is will always come into question, but no matter.

The circumference of a circle based upon that measurement (as a radius) is 7.689 * pi * 2.

>>> 7.689 * pi * 2
48.311411826903836
>>> 

But, the gotcha here is that we cannot use that precise a number when the given measurement as only 3 place precision. We have to round to arrive at the correct precision…

>>> round(7.689 * pi * 2, 3)
48.311
>>> 

Were we to force the presicion of pi to only one extra digit for calculating the outcome,

>>> round(7.689 * round(pi, 4) * 2, 3)
48.312
>>> 

#13

why is it / instead of only one / ?, from what you´re saying I understand that the answer will be a non decimal response to the task, so that if 8/13 it will be 0, while adding 1 it gives it a non decimal part, so it only is =1
I don´t quite understand your point with the floor, ceiling and round, but I think it is the same ? ah ok, I got it, the round, if given a float or integer will return an integer, and what you´re saying is that if given an integer it will be the same and would only be waisting “clicks” (time)


#14

I don´t understand this at all…


#15

round() when given a float will return a float. It’s pointless to give it an integer. Integers cannot be rounded unless we are seeking, say, the nearest tens, or nearest hundreds. That takes a little more understanding of the arithmetic involved. Something we should not get into too deep while you are just starting out. This topic has already provided enough of a distraction.

If you understand that a float takes the form, 0.0 and an integer takes the form 0 you have enough knowledge moving forward.

The floor of a float is the closest integer when we round down, and the ceiling of a float is the closest integer when we round up. round does not change the type, but floor and ceil do. In truth, neither of these functions will come up anywhere in this course. But the concepts may surface in some challenges.

keep pouring it on, and as you go, the community will be here to field your questions.

Since we are working in Python 2, remember that 8 / 13 is 0, and 15 / 100 is 0.


#16

In computing and mathematics, we look at the floating point precision in order to approximate as closely as needed to a true value. That true value is the accuracy. We can represent different orders of magnitude, again depending upon what is needed for the calculation.

Python gives us double precision IEEE-754 floats that give us 53 bits of precision. Even with this precision, we still are not exactly at the true value. There are some tricks we can use to increase the precision and drive ourselves to get closer and closer to that true value.

However, for the majority of the work we do, IEEE-754 floats is good enough. When I used calculate some very close vision coordinates for the robotic trajectories, we had to go further. But most of the time, we could get accurately do our work using the precision provided out of the box.

While your argument has validity, we must remember that it greatly depends upon how close you need to get to that absolute true value to achieve the accuracy needed.


#17

Circumference of a circle, C, is equal to 2 times the radius of the circle times PI. Under the rules of significant figures in scientific calculations, we cannot exceed the number of decimal places (plus 1) in values we introduce to the calculation. And, our reported solution cannot have more decimal places than the least precise value used.

10.789 * 1     == 10
10.789 * 1.0   == 10.8
10,789 * 1.00  == 10.79
10.789 * 1.000 == 10.789

We cannot arbitrarily add zeros to increase the precision. I just used them as placeholders to illustrate the outcome under each given precision. The number of least precision determines the precision of the reported outcome. If this is a secondary number to be used in further calculation, then we can increase place precision by 1 but must comform to above rule on output.

Here’s another example…

1.1 * 1.1 => 1.2
1.10 * 1.1 => 1.2
1.10 * 1.10 = 1.21

This is on paper, though. But when you have a science assignment that requires you to conform to sigdig rules, now you have the approach and the knowledge of how to do this with code.

>>> round(7.689 * 2 * round(pi, 4), 3)
48.312
>>>

Our field measurement is 7.689 mm, which we need to multiply by 2, and again by PI, but in this case we’ve set the place precision to one more than our permitted sigdig range. The final reported value is 3 place precision to match the input measurement’s place precision.

Bookmark this topic and keep on with the track so you can discover where these concepts will come up. Definitely return when you are doing review or looking for things to delve deeper into.


#18

What you are referring to as accuracy, I am referring to as nearness. A very close semantic term in this instance is exactness which floating point arithmetic can only hope for.

>>> 1 / 3 * 1
0.3333333333333333
>>> 1 / 3 * 2
0.6666666666666666
>>> 1 / 3 * 3
1.0
>>> 

The computer cannot see that three times one-third is unity. Only a derived float with more precision than is scientifically allowed. Though accurate, the exactness is lost.

Were I a science teacher (which I am not) I would accept both answers from above, 48.311 and 48.312 since we didn’t introduce PI as an arbitrary value in the first case, but as PI. For those students who chose to use the stricter rule, under the constraints they could not be faulted. The question of accuracy in either case is one of precision. I would hesitate to call either one accurate.

I do get where you are coming from, and I hope you get where I am coming from. We define accuracy in slightly different terms. I equate accuracy to inputs, not outputs. The measurements are handed to the calculations. This is Grade 10 general science I’m speaking of, though I fully appreciate the bigger picture in the world of engineering.


#19

2 posts were split to a new topic: Trouble with NOT


Trouble with NOT
#20

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.