-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should we allow int*float and int*decimal? #261
Comments
+1 for the Suggestion. Right now there is an ambiguity when we use numeric literals (1) vs variable reference (2). eg:
(1) works because Spec says, A numeric-literal represents a value belonging to one of the basic types int, float or decimal. The basic type to which the value belongs is determined as follows:
- if the numeric-literal includes a FloatTypeSuffix, then the basic type is float;
- if the numeric-literal includes a DecimalTypeSuffix, then the basic type is decimal;
- if the numeric-literal is a HexFloatingPointLiteral, then the basic type is float;
- otherwise, the basic type depends on the applicable expected numeric type (where the possible basic types are int, float and decimal):
-- if the applicable contextually expected type is a subtype of decimal, then the basic type is decimal;
-- if the applicable contextually expected type is a subtype of float, then the basic type is float;
-- otherwise, if the numeric literal is an int-literal, then the basic type is int;
-- otherwise, the basic type is float. |
I agree (1) should work, but I think spec needs to be clearer about this. If expression E is E1*E2, and the contextually expected type for E is decimal, then the contextually expected type for both E1 and E2 should also be decimal, but I don't see anything in the spec that says this. |
Similarly |
Technically, we can support all the above cases. |
There is no use of the contextually expected type to determine the operation. The operation is determined by the type of the operands. We are not changing the language to do implicit numeric conversions.
The only ones in your list that this issue covers are: 2, 4, 8, 9. |
Related to For an example double f = 0.1;
double i = 100000;
f = i * f;
System.out.println(f); // 100000
double f2 = 0;
for (int k = 0; k < 100000; k++) {
f2 += 0.1;
}
System.out.println(f2); // 10000.000000018848 probably this is not the result user expected |
You would implement |
If so, ballerina supports implicit numeric conversions for |
Do you have a question about how to implement this feature? |
Ah no. I was confused about followings..
IINM, we don't need to change the language to do implicit numeric conversions, because we are assuming |
I think considering |
Right. |
Are we going to allow |
No, we are not allowing We should |
Should the following be true?
according to commutative law a + b = b + a and ab = ba order of the operands should not affect the final result. I have tested this with Found some similar scenarios where the jballerina does not preserve commutative law ballerina-platform/ballerina-lang#35706. Should jballerina handle these cases? |
This is happening only for the |
IEEE floating point values are not real numbers and do not follow the laws that apply to real numbers in mathematics. It will happen with decimal too, because that also has precision limits. It is OK for x1 not to be equal to x2 in the above. |
One could view
3 * 3.5
as meaning to3.5 + 3.5 + 3.5
. In other words multiplication of a float by an int is an operation on a float, which is distinct from multiplication of two floats and which does not need to be viewed as requiring a conversion from int to float.For example, imagine that we represent a time duration as decimal seconds.
The text was updated successfully, but these errors were encountered: