4.1.2 float
Although most programs only use integers, they are unpractical when doing
trigonometric calculations, transformations or anything else where you
need decimals. For this purpose you use float. Floats are normally
32 bit floating point numbers, which means that they can represent very large
and very small numbers, but only with 9 accurate digits. To write a floating
point constant, you just put in the decimals or write it in the exponential
form:
3.14159265358979323846264338327950288419716939937510 // Pi
1.0e9 // A billion
1.0e-9 // A billionth
Of course you do not need this many decimals, but it doesn't hurt either.
Usually digits after the ninth digit are ignored, but on some architectures
float might have higher accuracy than that. In the exponential form,
e means "times 10 to the power of", so 1.0e9 is equal to
"1.0 times 10 to the power of 9".
All the arithmetic and comparison operators can be used on floats.
Also, these functions operates on floats:
- trigonometric functions
- The trigonometric functions are: sin, asin,
cos, acos, tan and atan.
If you do not know what these functions do you probably don't
need them. Asin, acos and atan are of course short for
arc sine, arc cosine and arc tangent. On a calculator they
are often known as inverse sine, inverse cosine and
inverse tangent.
- float log(float x)
- This function computes the natural logarithm of x,
- float exp(float x)
- This function computes e raised to the power of x.
- float pow(float|int x, float|int y)
- This function computes x raised to the power of y.
- float sqrt(float x)
- This computes the square root of x.
- float floor(float x)
- This function computes the largest integer value less than or equal to x. Note that the value is returned as a float, not an int.
- float ceil(float x),
- This function computes the smallest integer value greater than or equal to x and returns it as a float.
- float round(float x),
- This function computes the closest integer value to x
and returns it as a float.