Skip to content

Value mismatch with numpy on int(arr) // -float(arr) #2689

Closed

Description

@stress-tess

While working #2688, I updated the float array to include negatives which resulted in the following value mismatch with numpy. I don't see the mismatch at a glance, but I'll dig into it more after all the tests are converted

Value mismatches:         1 / 591
int64(array) // float64(array): np: [-0.000e+00 -1.000e+00 -2.000e+00 -2.000e+00 -3.000e+00 -3.000e+00
 -4.000e+00 -5.000e+00 -5.000e+00 -6.000e+00 -7.000e+00 -8.000e+00
 -8.000e+00 -9.000e+00 -1.000e+01 -1.100e+01 -1.200e+01 -1.300e+01
 -1.500e+01 -1.600e+01 -1.700e+01 -1.900e+01 -2.000e+01 -2.200e+01
 -2.400e+01 -2.600e+01 -2.800e+01 -3.000e+01 -3.300e+01 -3.600e+01
 -3.900e+01 -4.200e+01 -4.600e+01 -5.000e+01 -5.500e+01 -6.000e+01
 -6.700e+01 -7.400e+01 -8.200e+01 -9.200e+01 -1.050e+02 -1.200e+02
 -1.390e+02 -1.640e+02 -1.990e+02 -2.480e+02 -3.260e+02 -4.660e+02
 -7.930e+02 -2.426e+03  2.474e+03  8.410e+02  5.140e+02  3.740e+02
  2.960e+02  2.470e+02  2.130e+02  1.880e+02  1.680e+02  1.530e+02
  1.410e+02  1.310e+02  1.220e+02  1.150e+02  1.090e+02  1.030e+02
  9.800e+01  9.400e+01  9.000e+01  8.700e+01  8.400e+01  8.100e+01
  7.900e+01  7.600e+01  7.400e+01  7.200e+01  7.000e+01  6.900e+01
  6.700e+01  6.600e+01  6.400e+01  6.300e+01  6.200e+01  6.100e+01
  6.000e+01  5.900e+01  5.800e+01  5.700e+01  5.600e+01  5.500e+01
  5.400e+01  5.400e+01  5.300e+01  5.200e+01  5.200e+01  5.100e+01
  5.100e+01  5.000e+01  5.000e+01  4.900e+01]
ak: [-0.000e+00 -1.000e+00 -2.000e+00 -2.000e+00 -3.000e+00 -3.000e+00
 -4.000e+00 -5.000e+00 -5.000e+00 -6.000e+00 -7.000e+00 -8.000e+00
 -8.000e+00 -9.000e+00 -1.000e+01 -1.100e+01 -1.200e+01 -1.300e+01
 -1.500e+01 -1.600e+01 -1.700e+01 -1.900e+01 -2.000e+01 -2.200e+01
 -2.400e+01 -2.600e+01 -2.800e+01 -3.000e+01 -3.300e+01 -3.600e+01
 -3.900e+01 -4.200e+01 -4.600e+01 -5.000e+01 -5.500e+01 -6.000e+01
 -6.600e+01 -7.400e+01 -8.200e+01 -9.200e+01 -1.050e+02 -1.200e+02
 -1.390e+02 -1.640e+02 -1.990e+02 -2.480e+02 -3.260e+02 -4.660e+02
 -7.930e+02 -2.426e+03  2.474e+03  8.410e+02  5.140e+02  3.740e+02
  2.960e+02  2.470e+02  2.130e+02  1.880e+02  1.680e+02  1.530e+02
  1.410e+02  1.310e+02  1.220e+02  1.150e+02  1.090e+02  1.030e+02
  9.800e+01  9.400e+01  9.000e+01  8.700e+01  8.400e+01  8.100e+01
  7.900e+01  7.600e+01  7.400e+01  7.200e+01  7.000e+01  6.900e+01
  6.700e+01  6.600e+01  6.400e+01  6.300e+01  6.200e+01  6.100e+01
  6.000e+01  5.900e+01  5.800e+01  5.700e+01  5.600e+01  5.500e+01
  5.400e+01  5.400e+01  5.300e+01  5.200e+01  5.200e+01  5.100e+01
  5.100e+01  5.000e+01  5.000e+01  4.900e+01]

Activity

  1. added
    bug
    on Aug 18, 2023
  2. self-assigned this
    on Aug 18, 2023
  3. changed the title [-]Value mismatch with numpy on 1int(arr) // -float(arr)`[/-] [+]Value mismatch with numpy on `int(arr) // -float(arr)`[/+] on Aug 18, 2023
  4. stress-tess commented on Sep 7, 2023

    @stress-tess
    MemberAuthor

    wow, so these only differ in one spot. Index 36

    Numpy has -67 and arkouda has -66

  5. stress-tess commented on Sep 7, 2023

    @stress-tess
    MemberAuthor

    The fraction represented by the linspace at position 36 is -6/11, so this ends up being 36 // (-6/11) which seems like it would divide evenly to 66, but the floor division gives 67. Python and numpy agree on this

    >>> 36 / (-6/11)
    -66.0
    
    >>> 36 // (-6/11)
    -67.0
    
    >>> 36 // ak.array([-0.5454545454545454])
    array([-66])

    So it makes sense we are getting this wrong because we do

    return floor(numerator/denom);

  6. stress-tess commented on Sep 7, 2023

    @stress-tess
    MemberAuthor

    This seems to be a numeric precision issue when dividing by an infinite decimal. I found this much simpler example which shows this:

    >>> 1 // (-1/3)
    -4.0
    
    >>> 1 / (-1/3)
    -3.0

    I guess we just need to figure out what the c function used for integer division and use that

  7. stress-tess commented on Sep 7, 2023

    @stress-tess
    MemberAuthor

    So according to this stack overflow answer, it's not really floor divide but round towards zero. I'm hoping updating the one line to be trunc(numerator/denom); will do the trick

  8. removed their assignment
    on Nov 13, 2024
  9. self-assigned this
    on Oct 1, 2025
  10. added 7 commits that reference this issue on Oct 1, 2025
    6cdc2f1
    1959c3e
    84607e3
    ee94f26
    07ff8ac
    aa1141f
    6284081
  11. added a commit that references this issue on Oct 8, 2025
    a52037b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions