http://qs1969.pair.com?node_id=222013

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

approx(dsin(30)) approx(dcos(30))
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($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; } }