Skip to content

Float data types are unnecessarily widened, causing problems #767

@opeongo

Description

@opeongo

There seems to be a problem with float data types always being widened as a result of an expression, regardless of the need to do so. For example:

float a = 1;
float b = 2;
c = a * b;
print(c);
2.0d :double

This shows up as a problem when the result of the expression is used as an argument to a method that is expecting the narrower data type. The outcome is an exception that the method matching the signature of the wider variable cannot be found. For example:

Float.isInfinite(1f*2f);
Sourced file: inline evaluation of: ``Float.isInfinite(1f*2f); '' : Error in method invocation: Static method isInfinite(double) not found in class'java.lang.Float' : at Line: 1 : in file: inline evaluation of: ``Float.isInfinite(1f*2f); '' : Float .isInfinite ( 1f * 2f ) 

Caused by
    bsh.ReflectError: Static method isInfinite(double) not found in class'java.lang.Float'

The internal documentation for the Operators.promotePrimatives method says

        Promote the pair of primitives to the maximum type of the two.
        e.g. [int,long]->[long,long]

but it appears that floats are unconditionally promoted to doubles by this method. Even if both operands are float, they will both be promoted to double.

I would like to change this behaviour to be more like what the above comment says: only widen as much as required. This would allow the expression

Float.isIninifte(1f*2f);

to work as expected.

I notice that the Primitive.shrinkWrap method also does an unconditional promotion of float to double. This means that there are a few places in the code base that follow a policy of widening floats to doubles even when not required. Will there be an impact on other parts of the code it I put together a PR to make this change?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions