Page 1 of 1
Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 10:22 am
by Joris
Why is Debug 30/4 equal to 7 instead of 7.5 ?
It makes using a float always necassary even when I just wonna be able to detect if a division is integer or not ?
(Does that make sence ? I'm getting confused now.)
Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 10:46 am
by Marc56us
Joris wrote:Why is Debug 30/4 equal to 7 instead of 7.5 ?
It makes using a float always necassary even when I just wonna be able to detect if a division is integer or not ?
(Does that make sence ? I'm getting confused now.)
Force it (add
.0)
Code: Select all
a.f=30
b.a=4
Debug 30/4 ; (show 7)
Debug 30/4.0 ; (show 7.5)
Debug a/b ; (equal 7.5)
Code: Select all
If Int(30 / 4.0) = 30 / 4.0
Debug "Integer"
Else
Debug "Not integer"
EndIf

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 10:57 am
by PMV
In calculations, PB will use the variable type that matches the used variables.
That means if you have a division of just intenger, the result will be an integer.
If you need to convert on the fly from integer to float, as the calculation based
on only integer variables needs to be done in floatingpoints, you can add
"0.0 +" at the beginning of the calculation.
MFG PMV
Code: Select all
a.a=30
b.a=4
Debug a/b
Debug 0.0 + a/b
Edit:
If you just want to know if there is remainder after the calculation, use Mod()
Because Mod() is a function and expects floatingpoints, the integer variables
will be converted to floatingpoints when passed to Mod() and so no problems.
Code: Select all
a.a = 30
b.a = 4
If Mod(a, b)
Debug "not integer"
EndIf
Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 11:10 am
by TI-994A
Joris wrote:Why is Debug 30/4 equal to 7 instead of 7.5?
To avoid this, simply cast the output:
Code: Select all
Debug StrF(28/4) ; = 7
Debug StrF(30/4) ; = 7.5
Joris wrote:...I just wonna be able to detect if a division is integer or not?
An alternative approach:
Code: Select all
If Mod(28, 4)
Debug "Not Integer"
Else
Debug "Integer"
EndIf
; = Integer
If Mod(30, 4)
Debug "Not Integer"
Else
Debug "Integer"
EndIf
; = Not Integer
Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 1:32 pm
by Joris
In calculations, PB will use the variable type that matches the used variables.
That means if you have a division of just intenger, the result will be an integer.
A strange conclusion, to me.
Still confusing the need of using those tricks to get a correct value debugged, but yeah...
Thanks.
Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 1:44 pm
by infratec
In 'C' you have the same behaviour.
Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?
Posted: Wed Dec 16, 2015 3:09 pm
by DontTalkToMe
Joris wrote:
Still confusing the need of using those tricks to get a correct value debugged, but yeah...
It's not really a trick, this is how languages usually works.
30 and 4 are integers.
30.0 and 4.0 are floats.
Integer division results in integers, not floats.
Float division results in floats.
If one of the operands is a float, the other one is also converted to float so you get a float division.
Unless there is a bug in the compiler:
http://www.purebasic.fr/english/viewtop ... =4&t=59422