bugGNU Astronomy Utilities - Bugs: bug #61969, Incorrect output for integer...


You are not allowed to post comments on this tracker with your current authentication level.

bug #61969: Incorrect output for integer calculations

Submitter:  Pedram Ashofteh-Ardakani <pedram>
Submitted:  Tue 01 Feb 2022 05:42:43 AM UTC
Category:  Arithmetic Severity:  3 - Normal
Item Group:  Output not reasonable Status:  Fixed
Privacy:  Public Assigned to:  pedram
Open/Closed:  Closed

Tue 01 Feb 2022 10:32:08 AM UTC, comment #4: 

Sure Mohammad, I defined it here in task #16114 :-)

Pedram Ashofteh-Ardakani <pedram>
Group Member
Tue 01 Feb 2022 09:58:28 AM UTC, comment #3: 

That last example was great (and more clear!). Here is the reason behind this behavior (which is expected, but not intuitive):

Arithmetic reads every number with the smallest possible numerical data type (even accounting for the un-signed nature; when the integer isn't negative!).

Therefore in the last example, both '100' and '4' are read as an unsigned 8-bit integer. Therefore, since both inputs are 'uint8', the output is also 'uint8' and this numeric data type only takes values betweeen 0 to 255. Therefore, when the value you want to put inside of it is larger, the remainder of that value and 256 will be written there instead: (300 % 256 = 44). This is called Integer overflow. You can see it in practice with this basic C program:

#include <stdio.h>
#include <stdint.h>

  uint8_t a=100, b=3, c=a*b;
  printf("multiply: %u\n", c);
  return 0;

So effectively, this "bug" is the expected behavior! In large data analysis, this is important because a 100Mb file (that is 'uint8'; like binary maps!) can become 400Mb if stored as an 'int32'! We don't want to generically read all small integers as "wider" numbers.

This is something that has been in my mind for a long time! The Good news is that C library's 'errno.h' has a special error code for such range overflows: 'ERANGE'. We should add a check for this error code within arithmetic and if it happens, a warning should be printed that an overflow has happened (to standard-error; so its even printed with '--quiet'!).

Can you define a task for this Pedram? I am so busy with many meetings today and am affraid that I may forget.

I will close this bug and we'll follow this in the newly defined task ;-).

Mohammad Akhlaghi <makhlaghi>
Group administrator
Tue 01 Feb 2022 05:59:10 AM UTC, comment #2: 

I thought maybe this was related to bug #61940 Numbers with many decimals incorrectly been read as float32, so I tried with the most recent version, and the problem persists.

== version ==
Arithmetic (GNU Astronomy Utilities) 0.16.140-ede8

Pedram Ashofteh-Ardakani <pedram>
Group Member
Tue 01 Feb 2022 05:49:37 AM UTC, comment #1: 

Actually, the problem occurs for the most basic multiplications with results greater than about 300:

$ astarithmetic 100 3 x

Pedram Ashofteh-Ardakani <pedram>
Group Member
Tue 01 Feb 2022 05:42:43 AM UTC, original submission:  


Incorrect output for a combination of multiplication, subtraction and addition when using integers, but it works fine with float.

steps to reproduce

$ astarithmetic 51 1 - 21 x 1 + 1 -
Arithmetic finished in 0.000321 seconds

$ astarithmetic 51. 1 - 21 x 1 + 1 -
Arithmetic finished in 0.000276 seconds

Pedram Ashofteh-Ardakani <pedram>
Group Member


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by makhlaghi (Posted a comment)
  • -email is unavailable- added by pedram
  • -email is unavailable- added by pedram (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.


    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-02-01 makhlaghi StatusNone Fixed
        Assigned toNone pedram
        Open/ClosedOpen Closed
    2022-02-01 pedram Carbon-Copy- Added makhlaghi

    Back to the top

    Powered by Savane 3.12.
    Corresponding source code