in reply to Some simple math and absolute values

Hi,

I am not a math expert nor an algorithm expert nor a golfer, Perl or otherwise, but maybe something like this?

my $price = ($wt || 1) * 10; $price = 10 * $price/abs($price) if abs($price) < 10;

Output:

perl blrg.pl 2.3 Price: 23 Next Weight: .3 Price: 10 Next Weight: 0 Price: 10 Next Weight: -.3 Price: -10 Next Weight: -2.3 Price: -23 Next Weight:

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Some simple math and absolute values
by sidmuchrock (Novice) on May 23, 2020 at 14:40 UTC
    This was what I was missing last night! I kept trying to say if $wt/abs($wt) and it was dying in the 0 case, but by turning the 0 to $min in the possible total and then checking $total/abs($total) that solved. it. Thank you.
    $total = ($wt || $min) * $price; $total = $total/abs($total) * $price * $min if abs($wt) < $min +;