@@ -150,15 +150,20 @@ for w in (32,64,128)
150
150
151
151
$ BID (x:: AbstractString ) = parse ($ BID, x)
152
152
153
+ function tostring (x:: $BID )
154
+ # fills global _buffer
155
+ ccall (($ (bidsym (w," to_string" )), libbid), Cvoid, (Ptr{UInt8}, $ BID), _buffer, x)
156
+ end
157
+
153
158
function Base. show (io:: IO , x:: $BID )
154
159
isnan (x) && (write (io, " NaN" ); return )
155
160
isinf (x) && (write (io, signbit (x) ? " -Inf" : " Inf" ); return )
156
161
x == 0 && (write (io, signbit (x) ? " -0.0" : " 0.0" ); return )
157
- ccall (( $ ( bidsym (w, " to_string " )), libbid), Cvoid, (Ptr{UInt8}, $ BID), _buffer, x)
162
+ tostring ( x)
158
163
if _buffer[1 ] == UInt8 (' -' )
159
164
write (io, ' -' )
160
165
end
161
- normalized_exponent = nox ( ccall (( $ ( bidsym (w, " ilogb " )), libbid), Cint, ( $ BID,), x) )
166
+ normalized_exponent = exponent10 (x )
162
167
lastdigitindex = Compat. findfirst (equalto (UInt8 (' E' )), _buffer) - 1
163
168
lastnonzeroindex = Compat. findlast (! equalto (UInt8 (' 0' )), view (_buffer, 1 : lastdigitindex))
164
169
if - 5 < normalized_exponent < 6
@@ -210,13 +215,12 @@ for w in (32,64,128)
210
215
if n > length (DIGITS) - 1
211
216
n = length (DIGITS) - 1
212
217
end
213
- # rounded = round(x * exp10($BID(n)), RoundNearestTiesAway)
214
- rounded = @xchk (ccall (($ (bidsym (w," round_integral_nearest_away" )), libbid), $ BID, ($ BID,), x * exp10 ($ BID (n))), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
218
+ rounded = round (ldexp10 (x, n), RoundNearestTiesAway)
215
219
if rounded == 0
216
220
DIGITS[1 ] = UInt8 (' 0' )
217
221
return Int32 (1 ), Int32 (1 ), signbit (x)
218
222
end
219
- ccall (( $ ( bidsym (w, " to_string " )), libbid), Cvoid, (Ptr{UInt8}, $ BID), _buffer, rounded)
223
+ tostring ( rounded)
220
224
trailing_zeros = 0
221
225
i = 2
222
226
while _buffer[i] != UInt8 (' E' )
@@ -258,16 +262,19 @@ for w in (32,64,128)
258
262
end
259
263
return Int32 (1 ), Int32 (1 ), signbit (x)
260
264
end
261
- normalized_exponent = nox (ccall (($ (bidsym (w," ilogb" )), libbid), Cint, ($ BID,), x))
262
- # rounded = round(x * exp10($BID(n - 1 - normalized_exponent)), RoundNearestTiesAway)
263
- rounded = @xchk (ccall (($ (bidsym (w," round_integral_nearest_away" )), libbid), $ BID, ($ BID,), x * exp10 ($ BID (n - 1 - normalized_exponent))), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
264
- rounded_exponent = nox (ccall (($ (bidsym (w," ilogb" )), libbid), Cint, ($ BID,), rounded))
265
- ccall (($ (bidsym (w," to_string" )), libbid), Cvoid, (Ptr{UInt8}, $ BID), _buffer, rounded)
265
+ normalized_exponent = exponent10 (x)
266
+ rounded = round (ldexp10 (x, n - 1 - normalized_exponent), RoundNearestTiesAway)
267
+ rounded_exponent = exponent10 (rounded)
268
+ tostring (rounded)
266
269
i = 2
267
270
while _buffer[i] != UInt8 (' E' )
268
271
DIGITS[i - 1 ] = _buffer[i]
269
272
i += 1
270
273
end
274
+ while i <= n + 1
275
+ DIGITS[i - 1 ] = UInt8 (' 0' )
276
+ i += 1
277
+ end
271
278
pt = normalized_exponent + rounded_exponent - n + 2
272
279
neg = signbit (x)
273
280
return Int32 (n), Int32 (pt), neg
@@ -287,8 +294,8 @@ for w in (32,64,128)
287
294
Base. eps (x:: $BID ) = ifelse (isfinite (x), @xchk (nextfloat (x) - x, OverflowError, " $($ BID) value overflow" , mask= OVERFLOW), $ (_parse (T, " NaN" )))
288
295
289
296
# the meaning of the exponent is different than for binary FP: it is 10^n, not 2^n:
290
- # Base.exponent (x::$BID) = nox(ccall(($(bidsym(w,"ilogb")), libbid), Cint, ($BID,), x))
291
- # Base.ldexp (x::$BID, n::Integer) = nox(ccall(($(bidsym(w,"ldexp")), libbid), $BID, ($BID,Cint), x, n))
297
+ exponent10 (x:: $BID ) = nox (ccall (($ (bidsym (w," ilogb" )), libbid), Cint, ($ BID,), x))
298
+ ldexp10 (x:: $BID , n:: Integer ) = nox (ccall (($ (bidsym (w," ldexp" )), libbid), $ BID, ($ BID,Cint), x, n))
292
299
end
293
300
294
301
for (f,c) in ((:isnan ," isNaN" ), (:isinf ," isInf" ), (:isfinite ," isFinite" ), (:issubnormal ," isSubnormal" ))
0 commit comments