FPMath: A J2ME fixed-point math library

March 28, 2011 Math

Mobile devices don’t always come with native support for fractional or real numbers and matching math functions. During the development of Razor in 2005, I found myself in need of a small, fast and reliable math library and created FPMath, a 16.16 fixed point math library for the J2ME (Java) platform. This library is now released under the Modified BSD license and can be downloaded freely below.

Fixed point numbers are stored as ints, reserving the upper 16 bit for the signed integer part, and the lower 16 bit for the fractional part divided in 1/65536ths. The following functions are available:

  • abs
  • add
  • atan
  • atan2
  • ceil
  • cos
  • div
  • exp
  • floor
  • fraction
  • log
  • max
  • min
  • mul
  • pow
  • round
  • roundToInt
  • sin
  • srqt
  • sub
  • toFP
  • toInt
  • toString

All functions come with doxygen-annotated descriptions, that also include the function’s maximum epsilon error, as found during brute-force exhaustive testing. The library has been optimized thorougly for performance, while requiring only a total of 560 bytes for its lookup tables. Feel free to ask for additional details.


Comments (4)

August 7, 2011

Nice Library for math function

September 1, 2015

Hi, nice library. Any chance it can be converted for 32.32 support?

September 1, 2015

(or 24.8) ?

September 13, 2015

Thanks, R. No I’m afraid not. Doing so would require completely reimplementing most of the functions, in a language I no longer use these days.

Leave a comment