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?)
How would you like the /?
How does this sound:
Per operation:
If the coder uses / then promote to double precision, else if \ used, promote to dividend size.
If / is used and produces a final result, the quotient will truncated if the result is integer unless the coder forces Round() or similar.
If / is used and produces an interim result, the quotient is a double and thus impacts on the type-ing decision made for the next operation using that result.
If \ is used and either operand is a float, treat as / but truncate the result (final or interim) unless the coder specifies otherwise via something like Round().
?
Edit: I think this is similar to your safe method.
Per operation:
If the coder uses / then promote to double precision, else if \ used, promote to dividend size.
If / is used and produces a final result, the quotient will truncated if the result is integer unless the coder forces Round() or similar.
If / is used and produces an interim result, the quotient is a double and thus impacts on the type-ing decision made for the next operation using that result.
If \ is used and either operand is a float, treat as / but truncate the result (final or interim) unless the coder specifies otherwise via something like Round().
?
Edit: I think this is similar to your safe method.
Dare2 cut down to size
- netmaestro
- PureBasic Bullfrog

- Posts: 8453
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
netmaestro wrote:I voted Pascal, always promote to double. Let us know when you've got a beta version ready, I'm sure Fred is itching to report some bugs!
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Re: How would you like the /?
well, it's not guessingTrond wrote: 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.
http://www.xs4all.nl/~bluez/datatalk/pu ... evaluation
follow the link above to see what pb does (afaik and with some
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: How would you like the /?
That's about exactly the same as I'm doing in the old compiler.blueznl wrote:well, it's not guessingTrond wrote: 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.there's a rule behind it all
http://www.xs4all.nl/~bluez/datatalk/pu ... evaluation
follow the link above to see what pb does (afaik and with somesupport of fred, errors are mine, bugs are his
)
"that's about exactly"
hehe
hehe
Last edited by blueznl on Tue Aug 22, 2006 3:34 pm, edited 1 time in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
I vote the C++ way, but I think that is only possible if you have a strictly typed language, i.e. variable types are known at compile-time.
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents. (Nathaniel Borenstein)
http://www.wirednerd.com
http://www.wirednerd.com


