bugGNU Octave - Bugs: bug #45639, Formatting float as integer is not...

 
 

bug #45639: Formatting float as integer is not working any more

Submitted by:  None
Submitted on:  Mon 27 Jul 2015 05:11:40 PM UTC  
 
Category: InterpreterSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Incorrect Result
Status: InvalidAssigned to: None
Originator Name: Reiner SuikatOriginator Email: -unavailable-
Open/Closed: ClosedRelease: 4.0.0
Operating System: Any

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Mon 27 Jul 2015 05:54:28 PM UTC, comment #2:

This appears to be a regression and compatibility
problem. The change may have been made for Matlab
compatibility Matlab fprintf shows 3.4.

It is also not correct that Octave does not round:

Octave shows:

>> x=3.4;
>> printf('%1.0d round\n',x);

3 round

>> x=3.6;
>> printf('%1.0d round\n',x);

4 round

>>


Matlab does show the result as e-format:

>> fprintf('%1.0d round\n',x)

4e+00 round

>>


But, this is unrelated to the report that you raised.

do you still see a problem different from rounding?

Michael Godfrey <godfrey>
Project Member
Mon 27 Jul 2015 05:38:45 PM UTC, comment #1:

Hi, thanks for your bug report. I'm sorry that this change broke your existing code, but this change was in fact necessary to be compatible with Matlab. Matlab does in fact disregard the "%d" conversion specifier when the argument is a floating point value.

See the following announcement on the developers list: https://lists.gnu.org/archive/html/octave-maintainers/2014-04/msg00044.html

This change is also documented in the news for Octave 4.0.0:

> Integer formats used in the printf family of functions now work for
> 64-bit integers and are more compatible with Matlab when printing
> non-integer values. Now instead of truncating, Octave will switch
> the effective format to '%g' in the following circumstances:
>
> * the value of an integer type (int8, uint32, etc.) value exceeds
> the maximum for the format specifier. For '%d', the limit is
> intmax ('int64') and for '%u' it is intmax ('uint64').
>
> * round(x) != x or the value is outside the range allowed by the
> integer format specifier.
>
> There is still one difference: Matlab switches to '%e' and Octave
> switches to '%g'.


The best option for you to pursue is to make a point of ensuring that you arguments are integers when you want them to be printed as integers, using one of round, fix, floor, or ceil as appropriate.

Mike Miller <mtmiller>
Project Administrator
Mon 27 Jul 2015 05:11:40 PM UTC, original submission:

Up to the latest 3.6.x version the statement
printf("%d...", x)
would format x as an integer, regardless if it in fact was a float. (So 3.4 would print as 3). This is the same behaviour as I would expect to see in any other programming language as well.

Since version 4.0 the same statement will not round the number, it will print a floating point value when x is not an integer. (So it now prints 3.4 as 3.4) This causes my code to fail and is absolutely inconsistent with any programming language I know.

Also I have been unable to find a format conversion that would in fact produce the desired result.

Anonymous

 

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

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by godfrey (Posted a comment)
  • -unavailable- added by mtmiller (Posted a comment)
  • -unavailable- added by None (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 3 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Mon 27 Jul 2015 05:38:45 PM UTCmtmillerStatusNone=>Invalid
      Open/ClosedOpen=>Closed
      Operating SystemMicrosoft Windows=>Any

    Back to the top


    Powered by Savane 3.1-cleanup1