How would you like the /?

For everything that's not in any way related to PureBasic. General chat etc...

How to compute the / (division) operator?

Pascal (always promote to double)
2
17%
Pascal/PHP/VB (always promote to float, promote to double if one of the operands is double)
0
No votes
C/C# (Promote to float if one operand is float)
4
33%
PureBasic (Clever guessing)
5
42%
Almost safe and slow guessing
1
8%
 
Total votes: 12

Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

How would you like the /?

Post 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?)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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.
Dare2 cut down to size
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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:
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

:lol:
Dare2 cut down to size
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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:
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
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6175
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: How would you like the /?

Post 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 :-))
( 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... )
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: How would you like the /?

Post 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.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6175
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

"that's about exactly"

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... )
u9
User
User
Posts: 55
Joined: Tue May 09, 2006 8:43 pm
Location: Faroe Islands
Contact:

Post 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.
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
Post Reply