Menu

large number multiplied

Help
Mikam
2018-12-03
2018-12-03
  • Mikam

    Mikam - 2018-12-03

    Hi All
    val1, val2 and answer are dim as long
    val1 * val2 = answer

    if val1 = 255 and val2 = 255 calculated answer = 65025 'correct
    if val1 = 256 and val2 = 256 calculated answer = 0 'incorrect
    if val1 = 257 and val2 = 257 calculated answer = 513 'incorrect
    if val1 = 258 and val2 = 258 calculated answer = 1028 'incorrect
    looks like a carry over issue.

    GCB v98.03
    18F25k80 also tried with 18F45k80
    anyone seen this?
    Thanks

     
  • Anobium

    Anobium - 2018-12-03

    Without seeing your code.... I think this is not an issue.

    #chip 16f877a
    #option explicit
    'USART settings
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    
    dim val1, val2 as Word
    dim answer as long
    
    val1=257
    val2=257
    
    answer = val1 * val2
    
    HSerPrint answer
    

    My guess, you have not dimensioned to variables. Use #option explicit

     
  • Mikam

    Mikam - 2018-12-03
      #chip 18F45k80,32
      #option explicit
      #config OSC=INTIO2, MCLRE=OFF, WDT=OFF, BOR=OFF
    
        #include <glcd.h>
        #define GLCD_TYPE GLCD_TYPE_ILI9341
    
    'ver 03Dec18a
    '------------------------
    'GCB v0.98.03  2018-10-20
    '------------------------
        #define GLCD_DO  portc.5
        #define GLCD_SCK portc.3
        #define GLCD_DC portc.2
        #define GLCD_CS portd.1
        #define GLCD_RESET portd.0
        #define ILI9341_HardwareSPI
    
      GLCDRotate 3
    
      GLCDBackground = ILI9341_BLACK
      GLCDfntDefaultSize = 3
      GLCDCLS
    
      dim val1, val2 as Word
      dim answer as Long
      val1 = 255
      val2 = 255
    
      do Forever
        val1 = 255
        val2 = 255
        answer = val1 * val2
       GLCDDrawString (30,10,str(answer),ILI9341_RED)
        val1 = 257
        val2 = 257
        answer = val1 * val2
       GLCDDrawString (30,40,str(answer),ILI9341_RED)
        val1 = 258
        val2 = 258
        answer = val1 * val2
       GLCDDrawString (30,70,str(answer),ILI9341_RED)
    
         wait 1 S
      loop
    

    Not sure what i have done wrong?

     
  • David Stephenson

    Is the correct term "casting". So to get a long answer do the following
    answer= [long] val1*val2

    Otherwise the calculation will be done as word.

     
  • Mikam

    Mikam - 2018-12-03

    changed val1 and val2 too long, still get the same answer.

     
  • Anobium

    Anobium - 2018-12-03

    Use Str32() rather than str().....

     
  • Mikam

    Mikam - 2018-12-03

    Works. Thanks.

     
  • stan cartwright

    stan cartwright - 2018-12-03

    I tried your code and got the same result.
    same result if you dim val1,val2 as long.

     

    Last edit: stan cartwright 2018-12-03
  • Anobium

    Anobium - 2018-12-03

    The root cause of this needs to be looked at. Twice in the last few days the use of Str and Str32 was a cause of a user issue. The same root cause as Val and Val32.

    The root cause is the compiler being able to handle overloaded functions (not to be confused with overloaded subroutines). Currently Str and Str32 work around the root cause of the issue -as Str handles Byte and Word with Str32 handling Long and then StrInteger handling Integer.

    A potential answer is to have the compiler handle overloaded functions where Str will handle all variable types but until then please use the appropiate function.

    Anobium

     

Log in to post a comment.