
Account disable announcement
As of Friday, September 8, 2023, the SEI Secure Coding Wiki no longer provides the ability to sign up for new user accounts. In addition, all accounts that have not made contributions (e.g., comments, edits) to a space or page content will be disabled (accounts that have had recent activity will not be disabled).
If you have a specific need to keep your account active, or you feel your account has been disabled in error, please submit a message.
Access to view pages will remain for the public users without an account.
Active Account Requirements
- Actively contributing content to the wiki (e.g. page edits, comments)
- Demonstrated on-going need to access information contained within the external wiki
Rules
Back Matter
Contact us if you
- have questions about the Secure Coding wiki
- have recommendations for standards in development
- want to request privileges to participate in standards development
We acknowledge the contributions of the following folks, and we look forward to seeing your name here as well.
3 Comments
thiago glauco sanchez
Like others languages, is not a good idea compare floating point numbers in Perl. For solving that you can:
transform the numbers in strings and compare the strings according hte precision needs of yours:
sub are_equals {
#dont forget to sanitize $num1 and $num2 before using into sprintf
my ($num1, $num2, $precision) = @_;
return sprintf("%.${precision}f", $num1) eq sprintf("%.${precision}f", $num2);
}
define an acceptable error range ($delta) for your project:
sub are_equals {
my ($num1, $num2, $delta) = @_;
if ( $delta >= ($num1 - $num2) )
return 1;
return 0; }
Or just use bignum accordingly.
David Svoboda
Yes, there is a world of information on how to handle floating-point arithmetic. IIRC the Perl FAQ has some info. Also CERT has plenty of info on their C wiki: Rule 05. Floating Point (FLP). Since most languages use IEEE 754 for fp arithmetic, mostly the do's and dont's are language-independent
Anonymous
You'll have troubles if $num1 is "too much" less than $num2 (think calling are_equals(0, 2, 1)).
abs() helps here:
sub are_equals {
my ($num1, $num2, $delta) = @_;
if ( $delta >= abs($num1 - $num2) )
return 1;
return 0; }
I also find it more readable to put the difference in the left hand side and call $delta with a more expressive (IMHO) name:
sub are_equals {
my ($num1, $num2, $max_delta) = @_;
return abs($num1 - $num2) <= $max_delta;
}