Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 00:53 11 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Odd behaviour of MMBasic VAL() ?

Author Message
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 12:05pm 01 Feb 2018
Copy link to clipboard 
Print this post

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?

Edited by CaptainBoing 2018-02-02
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1671
Posted: 01:39pm 01 Feb 2018
Copy link to clipboard 
Print this post

  CaptainBoing said   Any ideas?

It smells like a bug. Get an exterminator. Edited by twofingers 2018-02-02
causality ≠ correlation ≠ coincidence
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3308
Posted: 02:15pm 01 Feb 2018
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 10569
Posted: 02:36pm 01 Feb 2018
Copy link to clipboard 
Print this post

  Quote   I will fix it in the next version.


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: Australia
Posts: 3308
Posted: 10:30pm 01 Feb 2018
Copy link to clipboard 
Print this post

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025