Skip to content

Commit 58325fe

Browse files
authored
don't throw InexactError on parsing/conversion (#94)
* don't throw InexactError on parsing/conversion * 1e10000 now parses to Inf
1 parent 4153c6b commit 58325fe

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/DecFP.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ macro xchk(x, exc, args...)
3939
end
4040
quote
4141
ret = $(esc(x))
42-
f = unsafe_load(flags[])
43-
unsafe_store!(flags[], 0)
44-
f & $mask != 0 && throw($exc($(map(esc,args)...)))
42+
if $exc === nothing
43+
unsafe_store!(flags[], 0)
44+
else
45+
f = unsafe_load(flags[])
46+
unsafe_store!(flags[], 0)
47+
f & $mask != 0 && throw($exc($(map(esc,args)...)))
48+
end
4549
ret
4650
end
4751
end
@@ -213,7 +217,7 @@ for w in (32,64,128)
213217
if isnan(x) && !isnanstr(s)
214218
throw(ArgumentError("invalid number format $s"))
215219
end
216-
return @xchk(x, InexactError, :parse, $BID, s)
220+
return @xchk(x, nothing)
217221
end
218222

219223
$BID(x::AbstractString) = parse($BID, x)
@@ -422,7 +426,7 @@ for w in (32,64,128)
422426
@eval promote_rule(::Type{$BID}, ::Type{$BID′}) = $BID
423427
end
424428
if w != w′
425-
@eval Base.convert(::Type{$BID}, x::$BID′) = @xchk(ccall(($(string("__bid",w′,"_to_","bid",w)), libbid), $BID, ($BID′,), x), InexactError, :convert, $BID, x, mask=INEXACT)
429+
@eval Base.convert(::Type{$BID}, x::$BID′) = @xchk(ccall(($(string("__bid",w′,"_to_","bid",w)), libbid), $BID, ($BID′,), x), nothing)
426430
end
427431

428432
# promote binary*decimal -> decimal, for consistency with other operations above

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ for T in (Dec32, Dec64, Dec128)
193193
TI = eval(Symbol(string("UInt", sizeof(T)*8)))
194194
@test bswap(xd) == reinterpret(T, bswap(reinterpret(TI, xd)))
195195

196-
@test_throws InexactError parse(T, "1e10000")
196+
@test parse(T, "1e10000") == T(Inf)
197197
@test_throws DomainError asin(xd)
198198
@test_throws DomainError sqrt(yd)
199199
@test_throws DomainError acosh(zd)
@@ -222,3 +222,6 @@ end
222222

223223
@test Float64(d64"1e100") == 1e100
224224

225+
# issue #93
226+
@test parse(Dec64, "3935767060.093896713") == d64"3.935767060093897e9" ==
227+
Dec64(d128"3935767060.093896713")

0 commit comments

Comments
 (0)