<p>Fixed point is essentially storing a number of units of a certain size instead of storing a significand and an exponent. However, if the units could be of any size, how big would the fixed-point units be in a typical <code>fixed</code> type of a programming language? C does not have fixed-point, so to use this one just stores an integer and divides by the number of units in a 1 to get the value, for example, one could store an angle as a <code>uint16_t x;</code> representing 65536ths of a turn, and call <code>sinpif(x*0x1p-15F)</code>. However, if a language were to implement proper fixed-point semantics, how could this be done? What would be a typical range and precision of the varying fixed-point types?</p>

<implementation><numbers><fixed-point>
How could fixed-point be implemented?
<p>Fixed-point is an integer with a statically-known decimal (or more generally, statically-known divisor). You can represent a fixed-point datatype as an &quot;representation&quot; integer type and &quot;divisor&quot; integer (kind of like a fixed-sized array, which is an &quot;element&quot; type and statically-known &quot;length&quot; integer). At runtime, the value is simply an integer, but the compiler uses the statically-known divisor to do proper conversion (e.g. to floating-point or string).</p>
<p>For example, imagine a language with this syntax</p>
<pre><code>type Currency = FixedPoint&lt;u32, 100&gt;

let currency1: Currency = 50.34  # represented as 5034
let currency2: Currency = 123.9  # represented as 12390
let currency3: Currency = currency1 + currency2  # represented as 17424
print(currency3)  # prints 174.24
</code></pre>
<p>Note that there isn't one &quot;fixed-point&quot; type, since different types may opt for different fixed points. The key difference between these types and a floating-point type, is that all instances of a particular fixed point type have the same &quot;decimal&quot; or divisor (whereas floating-points the decimal can be at different points at runtime).</p>
<hr />
<p>I actually don't know any real-world languages which implement this feature, though. The closest may be F#, which has <a href="https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure" rel="noreferrer">measurement types</a>. For your specific example, you could declare a <code>[&lt;Measure&gt;] type angle</code>, and then ensure that <code>int16&lt;angle&gt; n</code> is treated as <code>n 2*pi/65536</code> radians like so:</p>
<pre><code>[&lt;Measure&gt;] type angle

let toRadians (x: int16&lt;angle&gt;) = x * 2 * pi / 65536&lt;angle&gt;
</code></pre>
<p>Then you can't use <code>int16&lt;angle&gt;</code> wherever a unit type is expected, you must apply <code>toRadians</code> first.</p>

CC BY-SA 4.0
2023-05-20T18:57:58.597Z
2023-05-20T18:57:58.597Z
tarzh
CC BY-SA 4.0
2023-05-20T19:12:58.510Z
For real-world uses of fixed-point arithmetic, a good place to look is probably assembly languages for digital signal processing chips. Fixed point numbers are used a lot in DSP.
kaya3
CC BY-SA 4.0
2023-05-20T20:50:21.030Z
COBOL has fixed-point arithmetic. The reason some programmers care about fixed-point arithmetic is that it's used in the real world!
Gilles 'SO- stop being evil'
<h1>Make precision part of the type</h1>
<p>I’m not familiar with any general-purpose languages that use fixed point at all, but this is what PostgreSQL does. If you want to indicate that a column stores 15 digits, of which two are to the right of the decimal point, type it as <code>NUMERIC(15, 2)</code>. This way, each user can choose a reasonable scale for their application, instead of the language forcing one on them.</p>

CC BY-SA 4.0
2023-05-20T19:01:01.633Z
2023-05-20T19:01:01.633Z
Bbrk24
CC BY-SA 4.0
2024-08-13T15:13:02.093Z
It may be useful to look at languages like PL/I or COBOL, where fixed point types were expected to be used routinely.  A key requirement to make such types work well, I think, would be to recognize that given an expression like `x=y+z;` the type of the intermediate result should be controlled by the type of `x`, using outside-in rules rather than the inside-out rules used by newer languages.
supercat
<h1>GCC Fixed-Point Types</h1>
<p><a href="https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html" rel="nofollow noreferrer">GCC has fixed point types as an extension</a>. The types are <code>_Fract</code> and <code>_Accum</code> (A new language would designate them as just <code>fract</code> and <code>accum</code>) and could be combined with <code>long</code> <code>short</code> etc to specify precision, and <code>unsigned</code> to specify signedness.</p>
<h2><code>fract</code></h2>
<p>A <code>fract</code> would store a normalized value between <code>[0, 1)</code>. The increments would be <code>1/2^bits</code> where <code>bits</code> is bits of precision. If signed, one of the bits would be a sign bit so the increments would be twice as big.</p>
<h2><code>accum</code></h2>
<p>An <code>accum</code> would store half the bits as integer bits and half as fraction bits. The range would be <code>[0, 2^(bits/2))</code> but in increments of <code>1/2^(bits/2)</code>.</p>

CC BY-SA 4.0
2023-05-20T22:05:52.787Z
2023-05-20T22:05:52.787Z
CPlus
<p>Ada has fixed-point numbers: <a href="https://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-5-9.html" rel="nofollow noreferrer">https://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-5-9.html</a></p>
<p>The given examples are</p>
<pre><code>type Volt is delta 0.125 range 0.0 .. 255.0;

  -- A pure fraction which requires all the available
  -- space in a word can be declared as the type Fraction:
type Fraction is delta System.Fine_Delta range -1.0 .. 1.0;
  -- Fraction'Last = 1.0 – System.Fine_Delta

type Money is delta 0.01 digits 15;  -- decimal fixed point
subtype Salary is Money digits 10;
  -- Money'Last = 10.0**13 – 0.01, Salary'Last = 10.0**8 – 0.01
</code></pre>
<p>There's two alternatives; a normal fixed-point type uses a power of two as its delta, and a decimal fixed-point type uses a power of ten.</p>

CC BY-SA 4.0
2023-05-20T19:16:07.077Z
2023-05-20T19:16:07.077Z
prosfilaes
CC BY-SA 4.0
2023-05-20T19:17:26.667Z
The question is not what languages have fixed point but how you might implement it for your own language
mousetail 'he-him'
CC BY-SA 4.0
2023-05-20T19:21:00.460Z
@mousetail This shows the same details as the Lisp and SQL examples above. They're all examples of how the syntax could work, and certainly for binary fixed-point, the implementations pretty trivial; shift everything by the appropriate power of two on literals and conversions, don't worry about add and subtract, and do the obvious modifications on multiply and divide.
prosfilaes
<h1>Have a default precision, and allow changing it</h1>
<p>Lisp has several different types of floats. One is <code>long-float</code>, with arbritrary precision. For example:</p>
<pre class="lang-clisp prettyprint-override"><code>(SETF (EXT:LONG-FLOAT-DIGITS) 3322)
</code></pre>
<p>This sets the &quot;long float&quot; precision to 3322 binary digits (approximately 1000 decimal digits). By default, it's 64 binary digits.</p>
<p>After this, you can print out a long float to see that it works: <a href="https://tio.run/##S87JLC74/18j2DXETUHDNSLEysffz13XzcffMUTXxdPdMyRYU8HY2MhIk0sjLb8oN7FEoURBqc5NSUFDX8HQx0DBXFPz/38A" rel="nofollow noreferrer" title="Common Lisp – Try It Online">Try it online!</a></p>

CC BY-SA 4.0
2023-05-20T19:10:21.087Z
2023-05-20T19:10:21.087Z
naffetS
# How could fixed-point be implemented?
Posted by: username_0 on 2023-05-20
Fixed point is essentially storing a number of units of a certain size instead of storing a significand and an exponent. However, if the units could be of any size, how big would the fixed-point units be in a typical `fixed` type of a programming language? C does not have fixed-point, so to use this one just stores an integer and divides by the number of units in a 1 to get the value, for example, one could store an angle as a `uint16_t x;` representing 65536ths of a turn, and call `sinpif(x*0x1p-15F)`. However, if a language were to implement proper fixed-point semantics, how could this be done? What would be a typical range and precision of the varying fixed-point types?
**Tags:** implementation numbers fixed-point
---
## 5 answers
### Answer by username_1 on 2023-05-20. Score: 6
Fixed-point is an integer with a statically-known decimal (or more generally, statically-known divisor). You can represent a fixed-point datatype as an "representation" integer type and "divisor" integer (kind of like a fixed-sized array, which is an "element" type and statically-known "length" integer). At runtime, the value is simply an integer, but the compiler uses the statically-known divisor to do proper conversion (e.g. to floating-point or string).
For example, imagine a language with this syntax
type Currency = FixedPoint

let currency1: Currency = 50.34  # represented as 5034
let currency2: Currency = 123.9  # represented as 12390
let currency3: Currency = currency1 + currency2  # represented as 17424
print(currency3)  # prints 174.24

Note that there isn't one "fixed-point" type, since different types may opt for different fixed points. The key difference between these types and a floating-point type, is that all instances of a particular fixed point type have the same "decimal" or divisor (whereas floating-points the decimal can be at different points at runtime).

I actually don't know any real-world languages which implement this feature, though. The closest may be F#, which has measurement types. For your specific example, you could declare a [] type angle, and then ensure that int16 n is treated as n 2*pi/65536 radians like so:
[] type angle

let toRadians (x: int16) = x * 2 * pi / 65536

Then you can't use int16 wherever a unit type is expected, you must apply toRadians first.
> **Comments:**
> **username_2** (2023-05-20): For real-world uses of fixed-point arithmetic, a good place to look is probably assembly languages for digital signal processing chips. Fixed point numbers are used a lot in DSP.  
> **username_3** (2023-05-20): COBOL has fixed-point arithmetic. The reason some programmers care about fixed-point arithmetic is that it's used in the real world!  
---
### Answer by username_4 on 2023-05-20. Score: 5
# Make precision part of the type

I’m not familiar with any general-purpose languages that use fixed point at all, but this is what PostgreSQL does. If you want to indicate that a column stores 15 digits, of which two are to the right of the decimal point, type it as `NUMERIC(15, 2)`. This way, each user can choose a reasonable scale for their application, instead of the language forcing one on them.
> **Comments:**
> **username_5** (2024-08-13): It may be useful to look at languages like PL/I or COBOL, where fixed point types were expected to be used routinely.  A key requirement to make such types work well, I think, would be to recognize that given an expression like `x=y+z;` the type of the intermediate result should be controlled by the type of `x`, using outside-in rules rather than the inside-out rules used by newer languages.  
---
### Answer by username_0 on 2023-05-20. Score: 3
# GCC Fixed-Point Types

[GCC has fixed point types as an extension](https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html). The types are `_Fract` and `_Accum` (A new language would designate them as just `fract` and `accum`) and could be combined with `long` `short` etc to specify precision, and `unsigned` to specify signedness.

## `fract`

A `fract` would store a normalized value between `[0, 1)`. The increments would be `1/2^bits` where `bits` is bits of precision. If signed, one of the bits would be a sign bit so the increments would be twice as big.

## `accum`

An `accum` would store half the bits as integer bits and half as fraction bits. The range would be `[0, 2^(bits/2))` but in increments of `1/2^(bits/2)`.
---
### Answer by username_8 on 2023-05-20. Score: 3
Have a default precision, and allow changing it
Lisp has several different types of floats. One is long-float, with arbritrary precision. For example:
(SETF (EXT:LONG-FLOAT-DIGITS) 3322)

This sets the "long float" precision to 3322 binary digits (approximately 1000 decimal digits). By default, it's 64 binary digits.
After this, you can print out a long float to see that it works: Try it online!
---
### Answer by username_6 on 2023-05-20. Score: 2
Ada has fixed-point numbers: https://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-5-9.html
The given examples are
type Volt is delta 0.125 range 0.0 .. 255.0;

  -- A pure fraction which requires all the available
  -- space in a word can be declared as the type Fraction:
type Fraction is delta System.Fine_Delta range -1.0 .. 1.0;
  -- Fraction'Last = 1.0 – System.Fine_Delta

type Money is delta 0.01 digits 15;  -- decimal fixed point
subtype Salary is Money digits 10;
  -- Money'Last = 10.0**13 – 0.01, Salary'Last = 10.0**8 – 0.01

There's two alternatives; a normal fixed-point type uses a power of two as its delta, and a decimal fixed-point type uses a power of ten.
> **Comments:**
> **username_7** (2023-05-20): The question is not what languages have fixed point but how you might implement it for your own language  
> **username_6** (2023-05-20): @mousetail This shows the same details as the Lisp and SQL examples above. They're all examples of how the syntax could work, and certainly for binary fixed-point, the implementations pretty trivial; shift everything by the appropriate power of two on literals and conversions, don't worry about add and subtract, and do the obvious modifications on multiply and divide.  
---
