Page 1 of 1

How would you like the /?

Posted: Sat Aug 19, 2006 8:08 pm
by Trond
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?)

Posted: Sat Aug 19, 2006 11:24 pm
by Dare
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.

Posted: Sat Aug 19, 2006 11:27 pm
by netmaestro
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! :lol:

Posted: Sat Aug 19, 2006 11:40 pm
by Dare
:lol:

Posted: Sun Aug 20, 2006 3:23 am
by Flype
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! :lol:
:lol: :D :lol:

Re: How would you like the /?

Posted: Sun Aug 20, 2006 2:46 pm
by blueznl
Trond 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.
well, it's not guessing :-) 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 some :-) support of fred, errors are mine, bugs are his :-))

Re: How would you like the /?

Posted: Sun Aug 20, 2006 3:11 pm
by Trond
blueznl wrote:
Trond 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.
well, it's not guessing :-) 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 some :-) support of fred, errors are mine, bugs are his :-))
That's about exactly the same as I'm doing in the old compiler.

Posted: Mon Aug 21, 2006 8:40 pm
by blueznl
"that's about exactly"

hehe :-)

Posted: Tue Aug 22, 2006 2:13 pm
by u9
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.