Skip to content

Commit abca23e

Browse files
committed
Made _float_tointeger saturate when the result is too big/small
1 parent 070ec32 commit abca23e

File tree

3 files changed

+715
-708
lines changed

3 files changed

+715
-708
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Version 7.0.0
99
- Improved some debug error messages
1010
- Only use the stack for variables whose address is actually taken (thanks to Ada for this!)
1111
- Modified _BlockDevice data structure (used for littlefs, but someday may be used for fatfs too)
12+
- Make _float_tointeger() saturate when the result is too big/small
1213

1314
Version 6.9.10
1415
- Fix compilation error when GETWORD is replaced by MOV or MUL

sys/float.spin

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,19 @@ pri _float_tointeger(a=float, r) : integer=long | s, x, m
148148
'Convert float to rounded/truncated integer
149149

150150
(s,x,m) := _float_Unpack(a)
151-
if x => -1 and x =< 30 'if exponent not -1..30, result 0
151+
if x > 30
152+
m := (s) ? $8000_0000 : $7FFF_FFFF
153+
elseif x < -1
154+
m := 0
155+
else
152156
m <<= 2 'msb-justify mantissa
153157
m >>= 30 - x 'shift down to 1/2-lsb
154158
m += r 'round (1) or truncate (0)
155-
m >>= 1 'shift down to lsb
159+
m >>= 1 'shift down to lsb
156160
if s 'handle negation
157-
-m
158-
return m 'return integer
161+
-m
162+
163+
return m 'return integer
159164

160165

161166
pri _float_Unpack(single=float) : s, x, m | tmp

0 commit comments

Comments
 (0)