Feature - Rejected handle float = int/int. always calculate division as integers first then convert on storage

taltamir

Member
I have a math issue when using mafia
Code:
> ash float x = (1/2)
Returned: 0.0

> ash float x = (1.0/2) Returned: 0.5
in the 1/2 case it does
step 1: "integer divided by integer is 0.5 which is stored as an integer 0"
step 2: "convert integer 0 into float 0.0"

My request is. when mafia detects a division, first convert the numbers into float, then calculate, then check if it needs to be converted back or not.
that is
float x = 1/2
int y = 1/2
step 1: convert integers to float. 1 to 1.0. convert 2 to 2.0.
step 2: calculate 1.0/2.0 = 0.5
step 3: store as x unchanged. convert 0.5 to int and get 0 then store it as y

This will get identical results when doing
int = int/int
but will fix the problem with
float = int/int
 

fronobulax

Developer
Staff member
In other languages there are rules about operator precedence and when type conversions. I believe ash is generally compliant and I would be reluctant to make a change.

Given the expected precedence of division it is part of the definition that the result of dividing two integers will be truncated to an integer before doing anything with the result. Sometimes people want that behavior and the issue you identify is dealt with by the person writing the code - if you don't want to lose something when dividing integers, explicitly convert them before doing the division.
 

Ryo_Sangnoir

Developer
Staff member
You are also free to use the javascript parser, which does what you want for numbers:
Code:
> js 1/2

Returned: 0.5
 

Obeliks

Member
I don't think it would necessarily break operator precedence, but it would definitely break existing code.
in the 1/2 case it does
step 1: "integer divided by integer is 0.5 which is stored as an integer 0"
step 2: "convert integer 0 into float 0.0"

This is not what happens. The operation that is happening here is an Integer division, so it's:
step 1: "Integer 1 divided by integer 2 is 0"

It has nothing to do with the fact that it's "stored as an Integer" intermediary, that's just how the operation is defined.
 

taltamir

Member
This is not what happens. The operation that is happening here is an Integer division, so it's:
step 1: "Integer 1 divided by integer 2 is 0"
That line was me describing what integer division is
1/2 = 0.5 is just how math works
1/2 = 0 is how integer division works
I was not referring to any intermediaries, but to the mechanism of integer division itself
all data in a computer is stored. even if it is only in the processor cache.
 

Obeliks

Member
I'm just saying, what you are requesting is to do a "completely" different operation, not just "normal" division not working right. There is also integer division in "math", so "how math works" is a somewhat narrow view.

Anyway, I just thought this might help to understand why it might not be a good idea and has been rejected.
 
Top