Skip Menu |

This queue is for tickets about the Test-Deeply-Float CPAN distribution.

Report information
The Basics
Id: 126018
Status: resolved
Priority: 0/
Queue: Test-Deeply-Float

People
Owner: Nobody in particular
Requestors: ether [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: doc omission
"Test::Number::Delta and Test::Deep's num() can also compare floats with tolerance, but not in data structures." -- yes, but there's also Test::Deep::NumberTolerant, which can.
On Tue, 7 Aug 2018 23:15:33 GMT, ETHER wrote: Show quoted text
> "Test::Number::Delta and Test::Deep's num() can also compare floats > with tolerance, but not in data structures." -- yes, but there's also > Test::Deep::NumberTolerant, which can.
A rephrase is certainly in order. However. First: My need is to compare float/float and int/float with tolerance *anywhere* inside the data structure. Basically overriding the "==" operator for int/float and float/float, for the duration of comparing the data structure, should Perl allow it. Is there a way to do that with Test::Deep? Second: a second glance at Test::Deep pointed out my misunderstanding. num() is indeed able to be used inside any point in a data structure. In this aspect, your within_tolerance() does not offer a difference compared to num() or say approx() from Test::Approximate. Number::Tolerance just offers more ways to express the tolerance.
On 2018-08-08 08:03:54, PERLANCAR wrote: Show quoted text
> Is there a way to do that with Test::Deep?
Sure: sub mynum { within_tolerance($_[0], <your tolerance criteria>) } or you could even `local *num = \&mynum;`.
On Wed, 8 Aug 2018 18:00:51 GMT, ETHER wrote: Show quoted text
> On 2018-08-08 08:03:54, PERLANCAR wrote: >
> > Is there a way to do that with Test::Deep?
> > Sure: > > sub mynum { within_tolerance($_[0], <your tolerance criteria>) } > or you could even `local *num = \&mynum;`. >
I'm dense, could you show the full example? Here's how I used it in one of App::cryp::arbit's tests. #!/usr/bin/env perl use strict; use Test::More; use Test::Deeply::Float; my $got = [ { base_size => 0.2, buy => { exchange => "gdax", gross_price => 491.1, gross_price_orig => 491.1, net_price => 491.9, net_price_orig => 491.9, pair => "ETH/USD", }, forex_spread => 0, gross_profit => 1.8, gross_profit_margin => 1.83262064752596, net_profit => 1.6, net_profit_margin => 1.62634681845904, sell => { exchange => "indodax", gross_price => 500.1, gross_price_orig => 5001000, net_price => 499.9, net_price_orig => 4999000, pair => "ETH/IDR", }, trading_profit => 1.6, trading_profit_margin => 1.62634681845904, }, ]; my $expected = [ { base_size => 0.2, buy => { exchange => "gdax", gross_price => 491.10000001, # TEST DIFF gross_price_orig => 491.1, net_price => 491.9, net_price_orig => 491.9, pair => "ETH/USD", }, gross_profit_margin => 1.832620641752596, #TEST DIFF gross_profit => 1.8, trading_profit_margin => 1.62634681845904, trading_profit => 1.6, forex_spread => 0, net_profit_margin => 1.62634681845904, net_profit => 1.6, sell => { exchange => "indodax", gross_price => 500.1, gross_price_orig => 5001000, net_price => 499.9000001, # TEST DIFF net_price_orig => 4999000, pair => "ETH/IDR", }, }, ]; is_deeply_float($got, $expected); done_testing;