Why is Debug 30/4 equal to 7 instead of 7.5 ?

Just starting out? Need help? Post your questions and find answers here.
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Why is Debug 30/4 equal to 7 instead of 7.5 ?

Post by Joris »

Why is Debug 30/4 equal to 7 instead of 7.5 ?

Code: Select all

a.f=30
b.a=4
Debug 30/4
Debug a/b
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.)
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?

Post by Marc56us »

Joris wrote:Why is Debug 30/4 equal to 7 instead of 7.5 ?

Code: Select all

a.f=30
b.a=4
Debug 30/4
Debug a/b
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
:wink:
Last edited by Marc56us on Wed Dec 16, 2015 11:02 am, edited 1 time in total.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?

Post 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. :wink:

Code: Select all

a.a = 30
b.a = 4
If Mod(a, b)
  Debug "not integer"
EndIf
User avatar
TI-994A
Addict
Addict
Posts: 2733
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?

Post 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.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?

Post by infratec »

In 'C' you have the same behaviour.
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Why is Debug 30/4 equal to 7 instead of 7.5 ?

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