I have just completed work on Math::Trig::Units with Math::Trig::Degree, Math::Trig::Radian and Math::Trig::Gradian subclasses. Something that has always anoyed me is how float operations return 0.499999999998 or 5.00000000001 when the actual value is 0.5. The approx sub was written for this module.
Because of the limit on the accuracy of the vaule of Pi that is easily supported via a float you will get values like dsin(30) = 0.49999999999999945 when using degrees (or gradians). This can be fixed using the approx() function.
By default the approx sub will modify numbers so if we have a number like 0.499999945 with 6 9s or 0.50000012 with 6 0s the number will be rounded to 0.5. It also works on numbers like 5.250000001. This is useful when using degrees or gradians. In degrees these functions will return 0.5 as expected
The approx sub takes a second optional argument that specifies how many 0s or 9s in a row will trigger rounding. The default is 6.approx(dsin(30)) approx(dcos(30))
approx($num, 7); # will return 0.5 for 0.500000001 but 0.50000001 + if # that is passed as it only has 6 zeros.
Numbers that do not fulfill the requisite criteria are returned unchanged. For example 0.5000001 will not be rounded to 0.5 as it only has 5 0s.
sub approx { my ( $num, $dp ) = @_; $dp = 6; if ( $num =~ m/\d*\.(\d*?)(9{$dp,})\d*/ ) { my $exp = 10** (length $1 + length $2); return int(($num * $exp) +1 )/$exp; } elsif ( $num =~ m/\d*\.(\d*?)(0{$dp,})\d*/ ) { my $exp = 10** (length $1 + length $2); return int($num * $exp)/$exp; } else { return $num; } }


Replies are listed 'Best First'.  

Re: Fix floats like you do in your head
by mojotoad (Monsignor) on Dec 24, 2002 at 21:32 UTC  
by tachyon (Chancellor) on Dec 25, 2002 at 00:11 UTC  
by mojotoad (Monsignor) on Dec 25, 2002 at 01:07 UTC  
by tachyon (Chancellor) on Dec 25, 2002 at 09:48 UTC  
by mojotoad (Monsignor) on Dec 25, 2002 at 10:27 UTC  
 
Re: Fix floats like you do in your head
by toma (Vicar) on Dec 26, 2002 at 17:52 UTC  
by tachyon (Chancellor) on Dec 27, 2002 at 05:55 UTC  
by toma (Vicar) on Dec 27, 2002 at 06:48 UTC 