bugGNU Octave - Bugs: bug #62283, max (a, b) produces unintuitive...


bug #62283: max (a, b) produces unintuitive results when mixing integers and floating point values

Submitted by:  None
Submitted on:  Sun 10 Apr 2022 06:53:18 PM UTC
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Documentation
Status:  Confirmed Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  * Open Release:  * dev
Operating System:  * Any
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


Mon 11 Apr 2022 08:20:22 PM UTC, comment #4: 

See also bug #60784 and bug #60968 for example. Automatic type conversion has always been tricky and subjective.

Arun Giridhar <arungiridhar>
Project Member
Mon 11 Apr 2022 07:13:56 PM UTC, comment #3: 

This issue can manifest itself in many ways.
Maybe an idea to issue a warning with ID (that can be disabled/enabled) in an earlier stage:

warning: double (%g) truncated to (%d) on conversion to type %s

But I guess that opens a box of pandora.

A.R. Burgers <arb>
Mon 11 Apr 2022 06:31:02 PM UTC, comment #2: 

A possible documentation fix is attached for your review.

(file #53076)

Arun Giridhar <arungiridhar>
Project Member
Sun 10 Apr 2022 08:58:12 PM UTC, comment #1: 

Confirmed.  I re-titled the report to be a little clearer.  A agree that the behavior appears strange.  It is, however, explainable and also Matlab-compatible.

I can explain what is happening for this code

a = int8 (100);  b = 200;  max (a, b)

First, Octave/Matlab always returns a value of integer type when a mixed computation of floating point and integer values is used.
 For example,

y = int8 (2) + double (2)
y = 4
class (y)

When Octave calculates

max (int8 (100), double (200))

it respects this rule and returns a result of class int8.

The first part of the calculation is to force the floating point number to be of type int8 but since 200 is greater than the maximum representable value it just returns that max value which is 127

int8 (200)
ans = 127

After that, the rest of the calculation is straightforward

max (int8 (100), int8 (127))
ans = 127

Rik <rik5>
Project Administrator
Sun 10 Apr 2022 06:53:18 PM UTC, original submission:  

 a = int8 (100);  b = 200;  max (a, b)

Expected 200, but got 127.

Weird rule for integers. Better to say "cannot call max for mixing int8 and double".

Was not documented in max so this bug report to ask to document this weird result.



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

Attach Files:

Attached Files
file #53076:  doc.patch added by arungiridhar (1KiB - text/x-patch)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by arb (Posted a comment)
  • -email is unavailable- added by arungiridhar (Updated the item)
  • -email is unavailable- added by rik5 (Posted a comment)

    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.

    Only project members can vote.





    Follow 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-04-11 arungiridhar Attached File- => Added doc.patch, #53076
    2022-04-10 rik5 CategoryDocumentation => Octave Function
        Item GroupUnexpected Error or Warning => Documentation
        StatusNone => Confirmed
        Release6.4.0 => dev
        Operating SystemMicrosoft Windows => Any
        Summarymax (a, b) gives neither a nor b but a random third value => max (a, b) produces unintuitive results when mixing integers and floating point values

    Back to the top

    Powered by Savane 3.9