Menu

Does division round, truncate?

Help
MBB
2018-03-25
2018-03-26
  • MBB

    MBB - 2018-03-25

    In GCB when you divide variables, are the results rounded, truncated or something else?

    For example:
    aa = 25
    bb = 7
    aa / bb = 3.57 Does GCB round to 4 or truncate to 3 or does it do something else?

     
    • Chris Roper

      Chris Roper - 2018-03-25

      Rather then tell you it is better to show you so here is a test program:

      '
      ' Math Test
      '
      #chip 16F877A,20
      #option explicit
      
      Dim arga, argb as Integer
      
      'USART settings
       #define USART_BAUD_RATE 9600
       Dir PORTC.6 Out
       Dir PORTC.7 In
       #define USART_DELAY 1 ms
       #define USART_TX_BLOCKING
      
      arga = 25
      argb = 7
      
      HSerPrint arga
      HSerPrint " / "
      HSerPrint argb
      HSerPrint " = "
      HSerPrint arga / argb
      HSerPrint " Remainder "
      HSerPrint arga % argb
      
      End
      

      The resut from the Terminal is:
      ~~~
      25 / 7 = 3 Remainder 4
      ~~~

      Cheers
      Chris

       
  • stan cartwright

    stan cartwright - 2018-03-25

    @MBB it's integer, word or long word. No decimal point float in gcb.
    25/7=3

     
  • stan cartwright

    stan cartwright - 2018-03-25

    @MBB it's integer, word or long word. No decimal point float in gcb.
    25/7=3

     
  • MBB

    MBB - 2018-03-26

    Thanks!

     
  • Anobium

    Anobium - 2018-03-26

    And....from the Help: Setting Variables


    Divide or division: Great Cow BASIC support division. Division can set the global system variable SysCalcTempX to the remainder of the division. However the following simple rules apply. . If both of the parameters of the division are constants, the compiler will do the calculation itself and use the result rather than making the microcontroller work out the same thing every time. So, if there are two constants used, the microcontroller division routine does not get used, and SysCalcTempX does not get set. . If either of the parameters of the division are variables, the compiler will ensure the microcontroller do es the calculation as the result could be different every time. So, in the this case the microcontroller division routine does get used, and SysCalcTempX is set.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.