PrePAN

Sign in to PrePAN

Test::LimitDecimalPlaces Test module for floating-point number by limiting number of decimal places

Good

Synopsis

    use Test::LimitDecimalPlaces tests => 5; # Can specify the test plan.

    # Equality test by default number of decimal places
    limit_ok(1.2345678, 1.2345678, 'Test the same floating-point values.');

    # Inequality test by default number of decimal places
    limit_not_ok( 0.0000001, 0.0000002, 'Test different values' );

    # Equality test by specified number of decimal places
    limit_ok_by(0.00000001, 0.000000006, 8, 'Test similar value.') ."\n"; # number of decimal places is 8

    # Inequality test by specified number of decimal places
    limit_not_ok_by( 0.00000001, 0.00000002, 8, 'Test different values.' ); # number of decimal places is 8

    # Compare arrays
    my @x = ( 0, 1, 0.1, 0.0000001, 0.0000001 );
    my @y = ( 0, 1, 0.1, 0.0000001, 0.00000006 );
    limit_ok(\@x, \@y, 'Compare arrays.');

    # Set a different default number of decimal places
    use Test::LimitDecimalPlaces num_of_digits => 6, tests => 1;
    limit_ok(1.234567, 1.234566, 'Test the similar floating-point values.');

Description

If compare floating point numbers normally, we cannot get the correct result on some environment. This module was made to solve this problem.

This module provides test functions that can compare numerical values by limiting number of decimal places. These functions are using splintf() internally to limit number of decimal places.

I'm worried

I think this module name is not precise and attractive. And function names are not cool and intuitive (I feel they are not grammatical in the first place. Do they make sense?).

I'm not good at English as you suspect. Please give me some advice.

Comments

This module already exists, in a mathematically robust form: https://metacpan.org/module/Test::Number::Delta
In my understanding, Test::Number::Delta tests equality (or inequality) by checking absolute value of the remainder between values is included in acceptance region (Is what I'm saying correct?).
Test::LimitDecimalPlaces uses sprintf() internally to limit number of decimal places, so this method is different from Test::Number::Delta.
Is approach of Test::LimitDecimalPlaces nonsense?

Thank you.
> Is approach of Test::LimitDecimalPlaces nonsense?

Well it could be improved by making it into Test::LimitSigfigs
https://en.wikipedia.org/wiki/Significant_figures

To pick a silly example: it wouldn't be reasonable to compare two hairs, width measured in meters, to three decimal places. (The numbers involved will be 0 in the first three decimal places.)

If I were comparing floats, I would write my own in preference to either of these libraries. It's not tricky, and I think the clarity is useful. T:N:D says (v1.03)

| With no arguments, epsilon defaults to 1e-6. (An arbitrary choice on the author's part.)

I would have epsilon somewhere around max(args) * 1e-8 for many applications, i.e. around 8 sigfigs.

hth,
Thank you for your advice. It was very useful.

Test::LimitSigfigs sounds cool. I will try to implement it.

Please sign up to post a review.