Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Maths : getting a float answer from integers

Author
Message
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 24th Feb 2003 13:23
a=10
b=4

what is best way to get proper float answer of a/b to get 2.5 and not have answer interpretted as 2

I can only get to work like:

c#=(1.0*a)/(1.0*b)

Is there a more logical way?
Gronda, Gronda
Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 24th Feb 2003 13:30
nope not really...

however you only need to float one value, and remember the .0's are the dot point value

so like
a = 10 : b = 4
c as float = (1.0*a)/b

c = 2.5

whereas
a = 10 : b = 4
c as float = (1.00*a)/b

c = 2.50



Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 24th Feb 2003 13:43
Ta very much.

I did consider just changing all my variables to floats to start with but I assume keeping as many variables as integers as possible makes things faster.

There is only a couple of occasions I need to know a float answer by dividing two integers.

Gronda, Gronda
Rob K
Retired Moderator
22
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 24th Feb 2003 15:22
that is a really lame bug in DBP grrr.

NOBODY has a forum name as stupid as Darth Shader. I do.
Richard Davey
Retired Moderator
23
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 24th Feb 2003 15:32
Sorry but how is the fact you've got an "integer divided by an integer returning an integer" a bug???!

How can they return anything else! Darth - of all people I would have hoped you'd realise this!!

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
Rob K
Retired Moderator
22
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 24th Feb 2003 17:26
No if I am putting the resultant value into a float then the floating point should be included, it should only be taken away or ignored if putting the resultant value into an integer variable - all division operations should be floating point by default and the outcome determined by the container variable type, not the participants' assumed type.

value = 2/4

should give 0

value# = 2/4

should give 0.5 because the container variable has room for it. All other languages do this.

however value# = 2/4 currently gives 0, but value# = 2.0/4 gives 0.5.

NOBODY has a forum name as stupid as Darth Shader. I do.
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 24th Feb 2003 17:44
I have to partially agree with Darth Shader, if I wanted the rounded integer value, I would have said:

a=10 : b=4 : c=a/b

if however I wanted float answer, I naturally would say:

a=10 : b=4 : c#=a/b

I can not think of any reason to want rounded value put in a float 'container', i.e. in above example, putting 2 into c# instead of 2.5

Having to multiply one of the values by 1.0 to fool db to do a floating point calculation is a bit much and is not documented anywhere. I found out by accident! What are noobies going to do?

Gronda, Gronda
Rob K
Retired Moderator
22
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 24th Feb 2003 19:16
Mainly also, Visual Basic, Delphi and I believe C++ behave in the manner which I have described above. Because DBPs Debugger has a sh*t variable watcher which generally crashes when debugging programs with non-standard types, lots of arrays and anything over 800 or so lines, this bug, unless you are aware of it can cause a lot of problems, particularly if the values are read in from external data.

float# = int/int should always return a float, int = int/int should always return an integer.

NOBODY has a forum name as stupid as Darth Shader. I do.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Feb 2003 19:31
Nope, your wrong Darth. C and C++ definitely don't do this. Delphi (as far as I can remember) doesn't either. VB does however.

In fact, most compilers will deal with values as they are received.

float x=1/2;
becomes
load integer 1
load integer 2
divide the numbers
promote to float
store in x

as opposed to:
load integer 1
promote to float
load integer 2
promote to float
divide the numbers
store in x

The idea being that you only convert format when you have to.
Richard Davey
Retired Moderator
23
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 24th Feb 2003 19:37
Sorry Darth that isn't true.

C# code (from a button function):


c will always return a 2 (and no, ToString() doesn't perform any modification to it).

The same code in DB Classic returns a 2.

C++ returns a 2.

Show me a language that does it differently please! This is NOT a bug.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
heartbone
22
Years of Service
User Offline
Joined: 9th Nov 2002
Location:
Posted: 24th Feb 2003 19:52
In FORTRAN (the grandaddy of the scientific/mathematic procedural languages)

INT1= 10
INT2= 4
FLOVAL= INT1/INT2

FLOVAL= 0.20000E+1
not 0.25000E+1

This is the model for later languages.
If M$VB is not consistant with FORTRAN, that's too bad.

The more you see, the more you know.
The more you know, the more you see.
Rob K
Retired Moderator
22
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 24th Feb 2003 21:10
OK fair enough people, my mistake about Delphi. It just seems to me that VBs way of doing it is far more sensible.

NOBODY has a forum name as stupid as Darth Shader. I do.
Richard Davey
Retired Moderator
23
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 25th Feb 2003 02:03
But non-standard.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
Shadow Robert
22
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 25th Feb 2003 06:23
thing is Shader if you want this added do you also want the speed taken away that Visual Basic has?

Think about it, you'd need to create 3 float instances ... which might not seem like alot but to enable it DBS would most likely have to make ALL integers as floats.

so rather than 1 process for the FPU, you now have 3 and zero for the standard Processing ... and we all know tha Professional is hardly x86 extension optimised, so this'll hit game speed hard.

Personally i prefer having it the way it is

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?

Login to post a reply

Server time is: 2025-05-20 15:08:12
Your offset time is: 2025-05-20 15:08:12