```    1:- module(ieee_754, [ieee_754_float/3]).    2
3:- use_module(library(clpfd)).    4
5:- use_module(library(canny/maths)).    6:- use_module(library(canny/bits)).```
ieee(Bits, ExpBits, ExpBias) is semidet
IEEE 754 has, for floating-point numbers of Bits wide, ExpBits exponent bits with bias of ExpBias. The bias applies to the integral base-2 exponent and determines its zero value.

Supports binary formats only. Does not support decimal formats.

```   16ieee(16, 5, 15).
17ieee(32, 8, 127).
18ieee(64, 11, 1023).
19ieee(128, 15, 16383).
20ieee(256, 19, 262143).```
inf(Bits, Inf) is semidet
Infinity has all exponent bits set and a zero significand. IEEE 754 distinguishes between positive and negative infinity using the sign bit.
```   28inf(Bits, Inf) :-
29    ieee(Bits, ExpBits, _),
30    Inf is ((1 << ExpBits) - 1) << (Bits - ExpBits - 1).```
ieee_754_float(+Bits, ?Word, ?Float) is det
ieee_754_float(-Bits, ?Word, ?Float) is nondet
Performs two-way pack and unpack for IEEE 754 floating-point numbers represented as words.

Not designed for performance. Uses CLP(FD) for bit manipulation. and hence remains within the integer domain. Float arithmetic applies outside the finite-domain constraints.

Arguments:
 Word - is a non-negative integer. This implementation does not handle negative integers. Negative support implies a non-determinate solution for packing. A positive and negative answer exists for any given Float. Sig - is the floating-point significand between plus and minus 1. Uses Sig rather than Mantissa; Sig short for Significand, another word for mantissa.
```   51ieee_754_float(Bits, Word, Float) :-
52    var(Float),
53    !,
54    sig_exp(Bits, Word, Sig, Exp),
55    ldexp(Sig, Float, Exp).
56ieee_754_float(Bits, 0, 0.0) :- ieee(Bits, _, _), !.
57ieee_754_float(Bits, Inf, +1.0Inf) :- !, inf(Bits, Inf).
58ieee_754_float(Bits, Inf, -1.0Inf) :-
59    !,
60    inf(Bits, Inf0),
61    Inf is 1 << (Bits - 1) \/ Inf0.
62ieee_754_float(Bits, NaN, 1.5NaN) :-
63    !,
64    inf(Bits, Inf0),
65    ieee(Bits, ExpBits, _),
66    NaN is Inf0 \/ (1 << (Bits - ExpBits - 2)).
67ieee_754_float(Bits, Word, Float) :-
68    frexp(Float, Sig, Exp),
69    sig_exp(Bits, Word, Sig * 2, Exp - 1).
70
71sig_exp(Bits, Word, Sig, Exp) :-
72    ieee(Bits, ExpBits, ExpBias),
73    SigBits is Bits - ExpBits - 1,
74    bits(Bits - 1, 1, Word, Sign, Word1),
75    bits(0, SigBits, Word1, Word0, Word2),
76    bits(SigBits, ExpBits, Word2, Exp0, 0),
77    Exp #= Exp0 - ExpBias,
78    sig(Sign, Word0, 1 << SigBits, Sig).
79
80sig(Sign, Word, Max, Sig) :- var(Sig), !, ieee_sig(Sign, Word, Max, Sig).
81sig(Sign, Word, Max, Sig) :- sig_ieee(Sign, Sig, Max, Word).
82
83ieee_sig(0, Word, Max, Sig) :- ieee_sig(Word, Max, Sig), !.
84ieee_sig(1, Word, Max, Sig) :- ieee_sig(Word, Max, Sig0), Sig is -Sig0.
85
86ieee_sig(Word, Max, Sig) :- Sig is Word / Max + 1.
87
88sig_ieee(1, Sig, Max, Word) :- sign(Sig) < 0, !, sig_ieee(-Sig, Max, Word).
89sig_ieee(0, Sig, Max, Word) :- sig_ieee(Sig, Max, Word).
90
91sig_ieee(Sig, Max, Word) :- Word is round((Sig - 1) * Max)```