PainterQubits / Unitful.jl

Physical quantities with arbitrary units
Other
613 stars 112 forks source link

Integer overflow when performing arithmetic with floating point quantities #510

Open CameronBieganek opened 2 years ago

CameronBieganek commented 2 years ago

Not sure if this is the same as #430.

using Unitful
using Unitful: kg, m, s, inch, minute, W, ft, rad

@unit hp "hp" HorsePower (74569987//100000)W false

x = 0.2450140015194046ft * inch^2 * minute^-1 * rad^-1
y = 494.70883675108837kg * inch^3 * m^2 * hp^-1 * minute^-1 * s^-3
julia> x^2 + y^2
ERROR: LoadError: OverflowError: 25000000 * 625000000000000 overflowed for type Int64
Stacktrace:
  [1] throw_overflowerr_binaryop(op::Symbol, x::Int64, y::Int64)
    @ Base.Checked ./checked.jl:154
  [2] checked_mul
    @ ./checked.jl:288 [inlined]
  [3] *(x::Rational{Int64}, y::Rational{Int64})
    @ Base ./rational.jl:334
  [4] power_by_squaring(x_::Rational{Int64}, p::Int64)
    @ Base ./intfuncs.jl:280
  [5] ^
    @ ./rational.jl:476 [inlined]
  [6] dpow
    @ ~/.julia/packages/Unitful/9janP/src/units.jl:205 [inlined]
  [7] basefactor(inex::Float64, ex::Rational{Int64}, eq::Int64, tens::Int64, p::Rational{Int64})
    @ Unitful ~/.julia/packages/Unitful/9janP/src/units.jl:234
  [8] basefactor
    @ ~/.julia/packages/Unitful/9janP/src/units.jl:258 [inlined]
  [9] map (repeats 3 times)
    @ ./tuple.jl:224 [inlined]
 [10] basefactor(x::Unitful.FreeUnits{(kg^2, hp^-2, inch^6, m^4, minute^-2, s^-6), 𝐋^6 𝐓^-2, nothing})
    @ Unitful ~/.julia/packages/Unitful/9janP/src/units.jl:261
 [11] #s57#155
    @ ~/.julia/packages/Unitful/9janP/src/conversion.jl:18 [inlined]
 [12] var"#s57#155"(::Any, s::Any, t::Any)
    @ Unitful ./none:0
 [13] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:580
 [14] promote_rule(#unused#::Type{Quantity{Float64, 𝐋^6 𝐓^-2, Unitful.FreeUnits{(ft^2, inch^4, minute^-2, rad^-2), 𝐋^6 𝐓^-2, nothing}}}, #unused#::Type{Quantity{Float64, 𝐋^6 𝐓^-2, Unitful.FreeUnits{(kg^2, hp^-2, inch^6, m^4, minute^-2, s^-6), 𝐋^6 𝐓^-2, nothing}}})
    @ Unitful ~/.julia/packages/Unitful/9janP/src/promotion.jl:74
 [15] promote_type(#unused#::Type{Quantity{Float64, 𝐋^6 𝐓^-2, Unitful.FreeUnits{(ft^2, inch^4, minute^-2, rad^-2), 𝐋^6 𝐓^-2, nothing}}}, #unused#::Type{Quantity{Float64, 𝐋^6 𝐓^-2, Unitful.FreeUnits{(kg^2, hp^-2, inch^6, m^4, minute^-2, s^-6), 𝐋^6 𝐓^-2, nothing}}})
    @ Base ./promotion.jl:289
 [16] _promote
    @ ./promotion.jl:326 [inlined]
 [17] promote
    @ ./promotion.jl:350 [inlined]
 [18] +(x::Quantity{Float64, 𝐋^6 𝐓^-2, Unitful.FreeUnits{(ft^2, inch^4, minute^-2, rad^-2), 𝐋^6 𝐓^-2, nothing}}, y::Quantity{Float64, 𝐋^6 𝐓^-2, Unitful.FreeUnits{(kg^2, hp^-2, inch^6, m^4, minute^-2, s^-6), 𝐋^6 𝐓^-2, nothing}})
    @ Unitful ~/.julia/packages/Unitful/9janP/src/quantities.jl:129
 [19] top-level scope
    @ ~/projects/FeedsAndSpeeds/src/test.jl:11
in expression starting at /home/cameron/projects/FeedsAndSpeeds/src/test.jl:11

At first I thought it might be because I used a rational number in the definition of hp, but I still get the same error if I define hp as follows:

@unit hp "hp" HorsePower 745.69987W false

If I convert the units before squaring and adding, then I don't get an overflow error:

julia> (x |> (inch^3/minute))^2 + (y |> (inch^3/minute))^2
9.08470807375845 inch^6 minute^-2
Socob commented 1 month ago

Contrary to #430 and #660, which involve unit conversions, this issue still present because it arises from a computation, not a conversion.