float_int32.anubis
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
The Anubis Project
Tools for float <-> Word32/Int
Author: Olivier Duvernois
Tools for manipulating Float to Word32/Int and Word32/Int to Float. Usefull for charts
where data, in floats, must be transformed into number of pixels
read tools/maybefloat.anubis
public define Maybe(Float)
to_mb_float
(
Int i
) =
success(to_Float(i)).
public define Maybe(Float)
to_mb_float
(
Word32 i
) =
success(to_Float(i)).
string_to_float(to_decimal(i)).
public define Maybe(Word32)
to_Word32
(
Maybe(Float) mf
) =
if mf is
{
failure then failure,
success(f) then success(truncate_to_Word32(integral_part(f))) //string_to_integer(float_to_string(f,9))
}.
public define Maybe(Int)
to_Int
(
Maybe(Float) mf
) =
if mf is
{
failure then failure,
success(f) then success(integral_part(f)) //string_to_integer(float_to_string(f,9))
}.
public define Maybe(Word32)
to_Word32
(
Float f
) =
success(truncate_to_Word32(integral_part(f))).
if string_to_integer(float_to_string(f,9)) is
{
failure then failure,
success(s) then
success(s+if decimal_part(f) is
{
failure then 0,
success(d) then if d<0.5 then 0 else 1
})
}.