How would you like the /?
Posted: Sat Aug 19, 2006 8:08 pm
Since I am working on a small compiler and am going to start with division (well, I already have division, but this is a rewrite of the compiler) I was wondering how I should implement it.
In Pascal, the / operator always returns a double precision floating point number and div is used for integer division.
In VB, the / operators always returns a float (I think) and \ is used for integer division. (My compiler will then use div to have \ available for other stuff.)
In C, as far as I have understood, the result is promoted (before calculation) to the type highest on this list that one of the operands has.
Double
Single
Long integer
Integer
In PureBasic, it appears as if the compiler attempts to guess. This is what I'm currently doing (and doing quite well), but since the new compiler is fundamentally different from the old one I'm not quite sure how to implement this.
The last way I can think of is to use a "safe guess": If one of the numbers in the expression is a floating point number, all divisions will be floating point.
--
The pascal way is easy to implement but requires a bit of work since an additional operator is required. It is also likely that many users will simply use / even when floating point precision is not necessary, thus making their programs slower than necessary.
The C way is dead simple to implement.
The PureBasic way is probably not too hard to implement.
The "safe guess" way is also simple to implement, but programs will suffer from unnecessary floating point divisions and the compilation time will go up. Also, it's a bit illogical.
--
I will give an example code:
A=5/3*3
In pascal, A will be 5, but you need to put in an explicit conversion from floating point back to integer. (If I do it the pascal way I'll do this automatically.)
In C, the result will be 3. This will also be 3: a = 5/3*3.0. To get 5, one of the operands must be float.
In PureBasic, the result will be 3, but this: a = 5/3*3.0 will be 5.
This: A=5/3*3+1.0 will be 4. (Should be 6.)
With the safe guess method, the result will be 3.
This: a = 5/3*3.0 will be 5.
And this: A=5/3*3+1.0 will be 6.
What should I do? (Or how would have done it?)
In Pascal, the / operator always returns a double precision floating point number and div is used for integer division.
In VB, the / operators always returns a float (I think) and \ is used for integer division. (My compiler will then use div to have \ available for other stuff.)
In C, as far as I have understood, the result is promoted (before calculation) to the type highest on this list that one of the operands has.
Double
Single
Long integer
Integer
In PureBasic, it appears as if the compiler attempts to guess. This is what I'm currently doing (and doing quite well), but since the new compiler is fundamentally different from the old one I'm not quite sure how to implement this.
The last way I can think of is to use a "safe guess": If one of the numbers in the expression is a floating point number, all divisions will be floating point.
--
The pascal way is easy to implement but requires a bit of work since an additional operator is required. It is also likely that many users will simply use / even when floating point precision is not necessary, thus making their programs slower than necessary.
The C way is dead simple to implement.
The PureBasic way is probably not too hard to implement.
The "safe guess" way is also simple to implement, but programs will suffer from unnecessary floating point divisions and the compilation time will go up. Also, it's a bit illogical.
--
I will give an example code:
A=5/3*3
In pascal, A will be 5, but you need to put in an explicit conversion from floating point back to integer. (If I do it the pascal way I'll do this automatically.)
In C, the result will be 3. This will also be 3: a = 5/3*3.0. To get 5, one of the operands must be float.
In PureBasic, the result will be 3, but this: a = 5/3*3.0 will be 5.
This: A=5/3*3+1.0 will be 4. (Should be 6.)
With the safe guess method, the result will be 3.
This: a = 5/3*3.0 will be 5.
And this: A=5/3*3+1.0 will be 6.
What should I do? (Or how would have done it?)