Game Development Reference
In-Depth Information
16 - AI Simulation
Try entering the following code into the interactive shell:
>>> spam = 100 / 4
>>> spam
25.0
>>> spam = spam + 20
>>> spam
45.0
>>>
Notice that in the above example, the data type of the value stored in spam is always a
floating point value. You can pass the floating point value to the int() function, which
will return an integer form of the floating point value. But this will always round the
floating point value down. For example, the expressions int(4.0) , int(4.2) , and
int(4.9) will all evaluate to 4 , and never 5 .
The round() Function
The round() function will round a float number to the nearest whole float number. Try
entering the following into the interactive shell:
>>> round(10.0)
10.0
>>> round(10.2)
10.0
>>> round(8.7)
9.0
>>> round(4.5)
5.0
>>> round(3.5)
4.0
>>> round(3.4999)
3.0
>>> round(2.5422, 2)
2.54
>>>
As you can see, whenever the fraction part of a number is .5 or greater, the number is
rounded up. Otherwise, the number is rounded down. The round() function also has an
optional parameter, where you can specify to what place you wish to round the number to.
For example, the expression round(2.5422, 2) evaluates to 2.54 .
Search WWH ::




Custom Search