|
Forum Index : Microcontroller and PC projects : Odd behaviour of MMBasic VAL() ?
| Author | Message | ||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
In recent fiddlings, I have noticed some problems with the action of VAL(). It certainly looks like a bug but maybe I am misinterpreting things. I have checked the description of VAL in the manual but there is no mention of this kind of behaviour. It seems to ignore characters for some reason and this affects its basic arithmetic (see example below) that VAL only returns the absolute value of binary and hex strings (probably octal as well but I didn't try) and has errors with different strings. I was rather expecting a binary string of sixty three 1's and a zero to return a value of -2 but it doesn't, also a value of eight FF's should be -1: > ? mm.ver 5.0408 > ? val("&b1111111111111111111111111111111111111111111111111111111111111110") 9223372036854775807 > ? val("&hffffffffffffffff") 9223372036854775807 <-- same value for different string This exhibits the same behaviour if I assign the VAL to a variable (the value is not assessed correctly either): > x%=val("&b1111111111111111111111111111111111111111111111111111111111111110") > ? x% 9223372036854775807 > ? bin$(x%,64) 0111111111111111111111111111111111111111111111111111111111111111 > However if I assign the same binary value directly, it behaves as I expected: > x%=&b1111111111111111111111111111111111111111111111111111111111111110 > ? x% -2 > Any ideas? |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1671 |
It smells like a bug. Get an exterminator. causality ≠ correlation ≠ coincidence |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
Good find, thanks. VAL() works correctly for numbers of 63-bits or less but not with a 64-bit number. I will fix it in the next version. Geoff Geoff Graham - http://geoffg.net |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10569 |
What is the bug? I looked at the source and couldn't see anything obvious unless strtoll() is faulty |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
Yes, stroll() is faulty. The replacement is: if(*p == '&') { p++; iret = 0; switch(toupper(*p++)) { case 'H': while(isxdigit(*p)) { iret = (iret << 4) | ((toupper(*p) >= 'A') ? toupper(*p) - 'A' + 10 : *p - '0'); p++; } break; case 'O': while(*p >= '0' && *p <= '7') { iret = (iret << 3) | (*p++ - '0'); } break; case 'B': while(*p == '0' || *p == '1') { iret = (iret << 1) | (*p++ - '0'); } break; default : iret = 0; } Geoff Graham - http://geoffg.net |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |