-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlonghex.red
More file actions
117 lines (99 loc) · 1.98 KB
/
longhex.red
File metadata and controls
117 lines (99 loc) · 1.98 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Red [
description: "Long bi-hexadecimal (radix 256, byte is a digit) numbers crude math. Keeps float! precision even with very big/small numbers."
see-also: "https://madformath.com/calculators/digital-systems/hexadecimal-arithmetic/hexadecimal-division-calculator-with-fractions/hexadecimal-division-calculator-with-fractions"
author: loziniak
]
longhex: context [
div: function [
main
by
] [
main: copy main
ret: copy #{}
ret-dec: 0
a: copy #{}
append a take main
while [all [
a <> #{00}
7 > length? ret ;-- 52 bits of float! precision. 52 / 8 = 6.5 ~= 7 byte-digits
]] [
while [a lt by] [
append a either tail? main [
ret-dec: ret-dec - 1
0
] [
take main
]
]
times: 0
until [
last-a: a
a: sub a by
; probe a: sub probe a probe by
; print ""
unless empty? a [times: times + 1]
empty? a ;-- sub zero
]
append ret times
; append ret probe times
; probe last-a
; print ""
a: last-a
]
reduce [ ;-- result = ret * (256 ^ (ret-dec * radix))
trim ret
ret-dec + length? main
]
]
sub: function [a b] [
a: copy a
b: copy b
if (length? a) < (length? b) [return copy #{}] ;-- less than 0
ret: copy #{}
borrowed?: no
until [
da: take/last a
if borrowed? [da: da - 1]
db: take/last b
if none? db [db: 0]
either da < db [
either empty? a [
return copy #{} ;-- less than 0
] [
da: 256 + da
borrowed?: yes
]
] [
borrowed?: no
]
insert ret (da - db)
empty? a
]
trim ret
]
compare: function [a b] [
if (length? a) < (length? b) [return -1]
if (length? b) < (length? a) [return 1]
forall a [
unless a/1 = b/1 [return a/1 - b/1]
b: next b
]
0
]
lt: make op! function [a b] [
0 > compare a b
]
gt: make op! function [a b] [
0 < compare a b
]
eq: make op! function [a b] [
0 = compare a b
]
trim: function [a] [
while [0 = first a] [
if 1 = length? a [break]
a: next a
]
a
]
]