sharkdp / numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
https://numbat.dev
Apache License 2.0
1.26k stars 53 forks source link

Panic when using `abs()` with a non-scalar argument #565

Closed Bzero closed 1 month ago

Bzero commented 2 months ago

The abs() function panics when attempting to use it on a non-scalar argument:

>>> abs(2)

  abs(2)

    = 2

>>> abs(2s)
thread 'main' panicked at numbat/src/ffi/math.rs:28:1:
called `Result::unwrap()` on an `Err` value: IncompatibleUnits(Product { factors: [UnitFactor { unit_id: UnitIdentifier { name: "second", canonical_name: CanonicalName { name: "s", accepts_prefix: AcceptsPrefix { short: true, long: false } }, kind: Base }, prefix: Metric(0), exponent: Ratio { numer: 1, denom: 1 } }] }, Product { factors: [] })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I think this is a regression as I am quite sure this used to work fine in the past.

sharkdp commented 2 months ago

Thank you for reporting this. Definitely worked before, yes. Pretty sure I broke it in #546 where I accidentally changed abs alongside with round/floor/.., which is not correct. abs should still be a

simple_polymorphic_math_function!(abs, abs);
Bzero commented 1 month ago

Thanks for the hint, I just opened #572.

I didn't bring back simple_polymorphic_math_function! just for this one function but implemented it directly, let me know if you would prefer otherwise.