bugGNU Octave - Bugs: bug #33168, abs function in octave c++ API...

 
 

bug #33168: abs function in octave c++ API converting standard C++ type to int

Submitter:  foehler <foehler>
Submitted:  Tue 26 Apr 2011 11:14:30 PM UTC
   
 
Category:  Libraries Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Other
Status:  Invalid / Not an Octave Bug Assigned to:  None
Originator Name:  foehler Open/Closed:  * Closed
Release:  * 3.4.0 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 05 Sep 2013 01:37:08 AM UTC, comment #2: 

This is not an Octave bug, this is simply confusion about C versus C++ functions. The plain abs function is from the C math library and is defined as "int abs(int)". To get a double-precision absolute value, use either std::abs as you have shown below or use the C fabs function.

Mike Miller <mtmiller>
Group Member
Tue 26 Apr 2011 11:23:30 PM UTC, comment #1: 

(follow up from previous post)

+verbatim
#include <octave/oct.h>
#include <cmath>

DEFUN_DLD (abs_test, args,,
  "Test abs")
{
double test=args(0).double_value();

octave_stdout<<test<<" :octave abs\t"<<abs(test)<<"\n";
octave_stdout<<test<<" :cmath  abs\t"<<std::abs(test)<<"\n";

return octave_value_list ();
}
+verbatim

This is the correct behaviour, considering the definition of 'abs'
here which only accept int-like var, and the definition of 'abs' here where only the octave type are allowed.

Defining double as 1x1 NDArray is not very convenient. If octave 'abs' can not accept double, should a warning be issued at compilation ( and suggest the <cmath> version ?)

However my C++ skill are limited and I am far from understanding the C++ API. Could someone confirm this ?

Kind regard,
 Fabrice

foehler <foehler>
Tue 26 Apr 2011 11:14:30 PM UTC, original submission:  

The abs function (loaded somewhere in <octave/oct.h>) converts to int as there is no definition of 'abs' using double but only int-like var.


#include <octave/oct.h>
#include <cmath>

DEFUN_DLD (abs_test, args,,
  "Test abs")
{
double test=args(0).double_value();

octave_stdout<<test<<" :octave abs\t"<<abs(test)<<"\n";
octave_stdout<<test<<" :cmath  abs\t"<<std::abs(test)<<"\n";

return octave_value_list ();
}
+verbatim

In octave
+verbatim
abs_test(-1.3)
+verbatim

This might be the correct behaviour considering the definition of abs  [http://octave.sourceforge.net/doxygen/html/functions_func_0x61.html#index_a here], which doesn't accept double, and the other definition of abs [http://octave.sourceforge.net/doxygen/html/pr-output_8cc.html here] which seems to use only int-like var as input.


Defining double variable as 1x1 NDarray  (or similar) is not very convenient. If the 'abs' behaviour can not be changed, could a warning be printed at compilation against this behaviour (and suggesting the cmath version)

However I am very far from understanding C++ API and I could be missing an important bit.

Could someone confirm this ?


Thanks,
 Fab.

(octave 3.4.0 compiled from source)

foehler <foehler>

 

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

Attach Files:
   
   
Comment:
   

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 mtmiller (Posted a comment)
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by foehler (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.

    Only group members can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2013-09-05 mtmiller StatusConfirmed Invalid / Not an Octave Bug
        Open/ClosedOpen Closed
    2012-02-27 rik5 Item GroupPerformance Other
        StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code