Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Problem with a sort result: weird subtraction behaviour?

by Discipulus (Canon)
on Jan 15, 2016 at 09:00 UTC ( [id://1152833]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Problem with a sort result
in thread Problem with a sort result

Infact it is something strange: while in motorbike this morning i thought i was wrong (in my previous post): the results of subtraction must be valid too when sorting: big minus less is greater than zero; equal minus equal is 0 and little minus big is less than zero:
perl -e "print join qq(\n),sort {$a - $b} @ARGV" 33 1 2 99 1 2 33 99 # same result using <=> or -
using big nums give me incorrect ordering using subtraction when one number is ten digits or more:
perl -e "print join qq(\n),sort {$a - $b} @ARGV" 10000000000 3 2 10000000000 2 3


L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^6: Problem with a sort result: weird subtraction behaviour?
by kzwix (Sexton) on Jan 27, 2016 at 14:32 UTC

    Discipulus, rdfield, you are both correct: This is a problem with long integers.

    What I don't understand is: Why are the integers correctly remembered, correctly displayed, but incorrectly compared to 0 ? Because, let's face it, it's the core of the problem:

    When it's a "small" number, it is correctly evaluated as positive, negative, or null.

    When it is a "big" number, then there may be problems in evaluating the sign. Much like if some bit from the big number was used as a sign bit, or whatever, where the real good practice would merely be to check the sign at the start of the number string...

    I mean, if it starts with a "+" or a digit, then it's positive. If it starts with a "-", it's negative. We keep that "sign", unless all digits are 0 -> in which case it is merely null.

    Did I miss something, there ?

      The problem is that the code that implements the behaviour is in C. There's no number string, there are different integer types with different byte sizes.
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-20 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found