Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:54 01 Aug 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 : mm2 Newbie Variable Questions

     Page 1 of 2    
Author Message
JackRabbit
Newbie

Joined: 21/07/2018
Location: United States
Posts: 14
Posted: 08:39pm 23 Jul 2018
Copy link to clipboard 
Print this post

Can I DIM a variable as an unsigned integer or should I just do:

myvar = &H00

Is there any way to define a variable as a byte or 16 bit word variable?

Is there any built-in functionality to parse variables into bytes?

I wrote a checksum calculation for the SHT31 temp/hum sensor and it does a lot of << and >> to lose excess bytes from the values so I'm thinking there may be a better way.
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1114
Posted: 01:40am 24 Jul 2018
Copy link to clipboard 
Print this post

JackRabbit,

There are only 3 types of variable in MMBasic:-

1. Floating point;

2. 64 bit signed integer; and

3. Character (string).

The MMBasic 2 User manual has a detailed explanation starting on page 34.

It explains how you can treat signed integers as unsigned but with limitations and caveats.

I don't believe there are any built in ways of doing either of your last two questions - others may have some ideas.

panky

... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
JackRabbit
Newbie

Joined: 21/07/2018
Location: United States
Posts: 14
Posted: 01:59am 24 Jul 2018
Copy link to clipboard 
Print this post

Thanks :) I have read that part of the manual, it says "64-bit unsigned numbers can be created using the &H, &O or &B prefixes to a number and these numbers can
be stored in an integer variable."

What wasn't clear to me was whether "created" included by means of a DIM.
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 02:22am 24 Jul 2018
Copy link to clipboard 
Print this post

It is good programming practise to always define/declare your variables and not just use them and have them created automatically.

You can use the OPTION EXPLICIT command in MM Basic to force this. That way any typos in a variable name do not become problems in your code.

So you would then use the DIM or Local (for Subs and Functions) as appropriate to create your variables.
 
JackRabbit
Newbie

Joined: 21/07/2018
Location: United States
Posts: 14
Posted: 03:20am 24 Jul 2018
Copy link to clipboard 
Print this post

Noted, Python and php have ruined me :(
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 03:35am 24 Jul 2018
Copy link to clipboard 
Print this post

You can leave out OPTION EXPLICIT and just have MM Basic create variables as you write them in your code (except for arrays they must be DIMensioned hence the name).

Perfectly fine for small programs. Tends to be a trip up as the program gets larger, more complicated or modified over time.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 05:18pm 27 Jul 2018
Copy link to clipboard 
Print this post

DIM would create a variable (of whatever type), whereas the part you quoted refers to numbers (via &H etc). The number is just that: a number. If you wish you can store it (i.e. hold it) in a variable.

A variable has a type (or is in effect any type as the original BASIC did things).

(There are also arrays.)

If you just need part of the 64 bits then you can either ignore the other bits or (for example) mask them off (typically using AND).

It's usually fairly easy to treat a variable as a byte (or as a 16-bit word) by ANDing with &Hff (or &Hffff).

You might like to post a code fragment in case someone offers a shorter version.

JohnEdited by JohnS 2018-07-29
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 12:05am 17 Jul 2020
Copy link to clipboard 
Print this post

I'm actually having an issue with this right now.

DIM a = &H00
DIM b = &H00
DIM c = &H00
DIM d = &H00

d = ((a^b) AND (a^c) AND &H00)
print d



when I run this I get
Domain error in pow
[6] d = ((a^b) AND (a^c) AND &H00)
Error: OverFlow


As far as I'm aware, "d" should be 0, considering all variables are 0.
I've tried various varieties of variable declarations. The only way this passes is if I DON'T declare the variables (literally, just "d = ((a^b) AND (a^c) AND &H00)", works).

Any ideas?
Edited 2020-07-17 10:07 by Atomizer_Zero
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 12:13am 17 Jul 2020
Copy link to clipboard 
Print this post

  Quote  I'm actually having an issue with this right now.


What version of MMBasic and what device?

Jim
VK7JH
MMedit
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 12:15am 17 Jul 2020
Copy link to clipboard 
Print this post

Thats a good point, as of right now I'm still stuck with MMBasic for Win/DOS. Sorry, I should have mentioned that.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 12:17am 17 Jul 2020
Copy link to clipboard 
Print this post

try declaring them as INTEGER
VK7JH
MMedit
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 12:23am 17 Jul 2020
Copy link to clipboard 
Print this post

  TassyJim said  try declaring them as INTEGER


THANK YOU. Seriously... I've been pulling my hair out for ages. I didn't even think to try it that way.

For clarity, this works fine.
DIM INTEGER a = &H0, b = &H0, c = &H0, d = &H0

d = ((a^b) AND (a^c) AND &H00)
print d


Thanks again!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 01:00am 17 Jul 2020
Copy link to clipboard 
Print this post

any number to power zero = 1
zero to any power = 0

zero to the power zero will always cause much heated discussion.
It should be classed as indeterminate but most software will treat it as one.

It looks like the DOS version has a problem with 0^0 for floats but OK for INTEGERs

Jim
VK7JH
MMedit
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 01:06am 17 Jul 2020
Copy link to clipboard 
Print this post

Be careful with the use of a type in a declaration.

DIM a=&H0, b=&Ho, c=&Ho, d=&Ho as integer

might declare a, b, and c as floating point variables and d as an integer variable.

DIM integer a=&H0, b=&Ho, c=&Ho, d=&Ho

probably makes a, b, c, and d integer varaibles.

DIM a%=&H0, b%=&Ho, c=&Ho, d%=&Ho

will makes a, b, and d integer variables, but it might make c a floating point.

The use of % or $ as a type specifier should be restricted to the declaraation statements. If you include either in assignment or comparison statements I believe that it will slow the execution of the code somewhat.

DIM i%, a%, b%

' this might be slower
' it forces the interpreter to check the types on each iteration
for i%=1 to 10000
 a%=i%+b%
next i%

' this might be faster
for i=1 to 10000
 a=i+b
next

Only Geoff knows for sure!

Paul in NY
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 581
Posted: 01:15am 17 Jul 2020
Copy link to clipboard 
Print this post

AFAIK (I think) integers on MM are always signed..
ie if the most significant bit is used it will result in a negative number. (?)

ed - or something similar, I do not know the details with MM.
Edited 2020-07-17 11:19 by zeitfest
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 01:16am 17 Jul 2020
Copy link to clipboard 
Print this post

Leave it to someone from Tasmania to bring up 0^0!  

If (a house)^0=1 and (a donkey)^0=1 does that mean that (a house)=(a donkey)????  

And, bye the way, does TAN(90°) really equal positive infinity and TAN(270°) equal negative infinity???????  

Paul in NY
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 581
Posted: 01:27am 17 Jul 2020
Copy link to clipboard 
Print this post

  zeitfest said  AFAIK (I think) integers on MM are always signed..
ie if the most significant bit is used it will result in a negative number. (?)

ed - or something similar, I do not know the details with MM.



or maybe the most negative number possible in MM is 63 bit ?
Experts please clarify ?
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 03:25am 17 Jul 2020
Copy link to clipboard 
Print this post

MMBasic uses signed integers

64 bit signed integers are 63 bits plus sign bit

That give you +-9.22x10^18
VK7JH
MMedit
 
Womble

Senior Member

Joined: 09/07/2020
Location: United Kingdom
Posts: 267
Posted: 11:52am 17 Jul 2020
Copy link to clipboard 
Print this post

  Paul_L said  Be careful with the use of a type in a declaration.

very true Paul.

This one has been biting me in the butt frequently when trying to port some of my old GW/QBasic code.

Geoff has a very good point when he put this in the manual:
When OPTION EXPLICIT is used (as recommended) the DIM, LOCAL or
STATIC commands are the only way that a variable can be created. If this
option is not used then using the DIM command is optional and if not used
the variable will be created automatically when first referenced.

  Paul_L said  The use of % or $ as a type specifier should be restricted to the declaration statements. If you include either in assignment or comparison statements I believe that it will slow the execution of the code somewhat.

This I did not know.  Makes sense though.

BTW. mixing DIM declarations, with Type Suffixes (!,%,$,etc.) and automatic variable creation constitutes a Really Bad Idea ... much swearing and head scratching will ensue.  Even for small programs, pick one method and stick to it.  Lots of comments might help, but you will end up confusing and frustraing yourself.

There is a reason Wombles are grey  
Edited 2020-07-17 21:53 by Womble
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 581
Posted: 09:30pm 17 Jul 2020
Copy link to clipboard 
Print this post

  Quote   "64-bit unsigned numbers can be created using the &H, &O or &B prefixes to a number and these numbers can
be stored in an integer variable."


What happens when the most significant bit is set, in MM ? Is the whole value used as a positive number ?
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025