TinyLine 2D Programming Guide

2 Fixed Point Numbers

TinyLine 2D uses an efficient fixed-point mathematics. A fixed-point data type is characterized by the word size in bits, the binary point, and whether it is signed or unsigned. The position of the binary point is the means by which fixed-point values are scaled and interpreted. Positive and negative values can also be represented as fixed-point numbers. One bit is used to hold the sign of the number.

TinyLine 2D supports fixed point and double fixed-point numbers. It corresponds to FIX_BITS and DFIX_BITS binary point. In other words, fixed-point numbers have FIX_BITS bits fraction length and double fixed-point numbers have DFIX_BITS bits fraction length. Thus, all numbers must be limited in range between -32,767.9999 to +32,767.9999.

The Tiny2D class contains methods for performing basic numeric operations with fixed-point precision numbers:

Name Description
abs(int) Returns the absolute value of a fixed point value.
max(int, int) Returns the greater of two fixed point values.
min(int, int) Returns the smaller of two fixed point values.
mul(int, int) Returns a fixed point number whose value is (a * b).
fastDistance(int, int) Returns the fast approximation for the Euclidean distance between two points, in 2D it is d = sqrt(x^2 + y^2).
div(int, int) Returns a fixed point number whose value is (a / b).
round(int) Returns the closest int to the argument.
sin(int) Returns the trigonometric sine of an angle.
cos(int) Returns the trigonometric cosine of an angle.
tan(int) Returns the trigonometric tangent of an angle.
atan2(int, int) Converts rectangular coordinates (dxdy) to polar (r, theta).

Fixed point arithmetic has advantages and of cause disadvantages as well.

Advantages:

  • Fixed point arithmetic is as fast as integer operations.
  • No special hardware required.

Disadvantages:

  • Limited range of values.
  • Loss of precision if intermediate result exceeds the maximum value.
  • Programmer must normalize results manually.

© 2010 TinyLine. All rights reserved.