Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Detecting whether UV fits into an NV

by syphilis (Bishop)
on Feb 27, 2020 at 05:57 UTC ( #11113471=note: print w/replies, xml ) Need Help??


in reply to Re^2: Detecting whether UV fits into an NV
in thread Detecting whether UV fits into an NV

I think that checking whether the UV arg == (UV)((NV)arg) is a splendid idea

Unfortunately this check fails for some UV values with less recent Microsoft Compilers - eg Visual Studio 2010 and earlier.
No problems with Visual Studio 2017 and Visual Studio 2019.
I suspect that Visual Studio 2015 is the oldest Microsoft Compiler that doesn't suffer from this problem, but I haven't confirmed that.

This particular issue looks like another manifestation of the problem that sent me down this path in the first place.
Here's the demo I ran:
use strict; use warnings; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; int uv_fits_double1( UV arg ) { if(arg == (UV)((NV)arg)) return 1; return 0; } int uv_fits_double2(UV arg) { while(!(arg & 1)) arg >>= 1; if(arg < 9007199254740993) return 1; return 0; } EOC my $ls = 63; my @in = ( 18446744073709549568, 18446744073709139968); for(@in) { print "$_: ", uv_fits_double1($_),uv_fits_double2($_), "\n" +; } __END__ With Windows Server 2003 SP1 Platform SDK, this outputs: 18446744073709549568: 01 18446744073709139968: 01 Base 2 representations of the 2 Uvs is (resp): 1111111111111111111111111111111111111111111111111111100000000000 1111111111111111111111111111111111111111111110011011100000000000 showing that both are exactly representable by a double.


Cheers,
Rob

Replies are listed 'Best First'.
Re^4: Detecting whether UV fits into an NV
by pryrt (Monsignor) on Feb 27, 2020 at 14:53 UTC
    Once you phrased it this way, I realized you finally gave me an in-the-wild use-case for my Data::IEEE754::Tools. Thanks! :-)

    #!/usr/bin/env perl use strict; use warnings; use Data::IEEE754::Tools qw/ulp/; sub uv_fits_double_using_ulp { 0 == ($_[0] % ($_[0]/ulp($_[0]))) } my @in = map { no warnings 'portable'; chomp; oct('0b'.$_) } <DATA>; for(@in) { my $d = $_/ulp($_); printf "%-24s: %4d %32.15f %16d %1s\n", $_, ulp($_), $_/ulp($_), $ +_ % $d, uv_fits_double_using_ulp($_) ? 'T' : 'F'; } __END__ 1111111111111111111111111111111111111111111111111111100000000000 1111111111111111111111111111111111111111111111111111110000000000 1111111111111111111111111111111111111111111111111111100000000001 1111111111111111111111111111111111111111111110011011100000000000 1111111111111111111111111111111111111111111111111111100000000000 0111111111111111111111111111111111111111111111111111110000000000 0011111111111111111111111111111111111111111111111111111000000000 0001111111111111111111111111111111111111111111111111111100000000 0000111111111111111111111111111111111111111111111111111110000000 0000011111111111111111111111111111111111111111111111111111000000 0000001111111111111111111111111111111111111111111111111111100000 0000000111111111111111111111111111111111111111111111111111110000 0000000011111111111111111111111111111111111111111111111111111000 0000000001111111111111111111111111111111111111111111111111111100 0000000000111111111111111111111111111111111111111111111111111110 0000000000011111111111111111111111111111111111111111111111111111 0000000000111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111111111111
    which gives
    18446744073709549568 : 2048 9007199254740991.000000000000000 + 0 T 18446744073709550592 : 4096 4503599627370496.000000000000000 450359 +9627369472 F 18446744073709549569 : 2048 9007199254740991.000000000000000 + 1 F 18446744073709139968 : 2048 9007199254740791.000000000000000 + 0 T 18446744073709549568 : 2048 9007199254740991.000000000000000 + 0 T 9223372036854774784 : 1024 9007199254740991.000000000000000 + 0 T 4611686018427387392 : 512 9007199254740991.000000000000000 + 0 T 2305843009213693696 : 256 9007199254740991.000000000000000 + 0 T 1152921504606846848 : 128 9007199254740991.000000000000000 + 0 T 576460752303423424 : 64 9007199254740991.000000000000000 + 0 T 288230376151711712 : 32 9007199254740991.000000000000000 + 0 T 144115188075855856 : 16 9007199254740991.000000000000000 + 0 T 72057594037927928 : 8 9007199254740991.000000000000000 + 0 T 36028797018963964 : 4 9007199254740991.000000000000000 + 0 T 18014398509481982 : 2 9007199254740991.000000000000000 + 0 T 9007199254740991 : 1 9007199254740991.000000000000000 + 0 T 18014398509481983 : 4 4503599627370496.000000000000000 450359 +9627370495 F 18446744073709551615 : 4096 4503599627370496.000000000000000 450359 +9627370495 F

    I doubt it's faster than some of hte others

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11113471]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2020-12-03 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (56 votes). Check out past polls.

    Notices?