[Bug 516] New: [Scripting]No conversions available from int -> double -> float etc.

For more infomation about this bug, visit
Summary: [Scripting]No conversions available from int -> double -
>float etc.
Product: RTT
Version: rtt-trunk
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: Real-Time Toolkit (RTT)
AssignedTo: orocos-dev [..] ...
ReportedBy: peter [dot] soetens [..] ...
CC: orocos-dev [..] ...
Estimated Hours: 0.0

There is sometimes a lot of confusion when a scripting method takes a float
argument and the user writes plain '3.0' (a double), which is rejected by the
parser.

In the RTT, it is possible to define constructor functions like:

var double d = double(4) // convert int to double

For these 1-to-1 conversions, the scripting engine could look up the
constructor table of double() itself and see if it is possible to construct a
double from an int. That would allow you to write:

var double d = 4 // automatic construction of a double from an int

A lot of newcomers have been bitten by this, and the constructor infrastructure
is already in place. Only the automatic lookup needs to be done at the right
places, which is a small patch.

We could have the following conversion table:

Automatic:
float ->[double]
double ->[float]
int ->[double, float]
char ->[int, uint]

Automatic, with a warning:
float -> [int, uint]
double-> [int, uint]
int -> [uint]
uint -> [int]

Any other suggestions ?

To supress the warnings, we could require the user to write C++ style casts:
var int i = int(3.333)
for example.

Peter

[Bug 516] [Scripting]No conversions available from int -> double

For more infomation about this bug, visit

Peter Soetens

<peter [dot] soetens [..] ...> changed:

What |Removed |Added
--------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution| |FIXED
Target Milestone|--- |1.6.0

--- Comment #3 from Peter Soetens

<peter [dot] soetens [..] ...> 2008-04-29 23:31:16 ---
The patches have been applied on trunk/rtt in r29199 and r29200.

[Bug 516] [Scripting]No conversions available from int -> double

For more infomation about this bug, visit

--- Comment #2 from Peter Soetens

<peter [dot] soetens [..] ...> 2008-04-19 23:21:37 ---
Created an attachment (id=262)
--> (https://www.fmtc.be/bugzilla/orocos/attachment.cgi?id=262)
automatic type conversion (almost) like in C++

This patch fixes this issue and allows you to write in Orocos scripts:

var double d = 3 + 4; // d == 7.0
or
d = 3.4 + 4; // d == 7.4
or even
d = float(3.4) + int(5.5); // d == 8.4

But if you write:
d = 3 + 4.4 // d == 7.0 (!)
You'll get a warning :
[ Warning][Logger] Conversion from double to int

This warning tells you that 4.4 has been converted to an int.
The first number (3, an int) 'leads' the operation and forces 4.4 to be
converted to int as well, because addition for ints is defined as (int) =
(int)+(int). To work around this, you must write:
d = double(3) + 4.4; // d == 7.4
which will cause the addition of doubles: (double) = (double)+(double) and the
warning will disappear.

This conversion also applies to methods, commands, constructors,etc. For
example, if a script method 'foo(double d)' has been defined, you can write
do foo( 3 ); // previously: do foo( 3. );
The integer 3 will be converted to a double automatically.

[Bug 516] [Scripting]No conversions available from int -> double

For more infomation about this bug, visit

Peter Soetens

<peter [dot] soetens [..] ...> changed:

What |Removed |Added
--------------------------------------------------------------------------
Status|NEW |ASSIGNED
AssignedTo|orocos- |peter [dot] soetens [..] ...
|dev [..] ... |

--- Comment #1 from Peter Soetens

<peter [dot] soetens [..] ...> 2008-04-19 13:28:48 ---
Created an attachment (id=261)
--> (https://www.fmtc.be/bugzilla/orocos/attachment.cgi?id=261)
Patch by Fabian Wiesel for creating uint and float values

This patch allows to create floats and uints in the scripting language.