Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:10 02 Jul 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 : dim var as byte

     Page 3 of 16    
Author Message
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4299
Posted: 02:39pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  STRING has 256 characters unless you DIM them using LENGTH.


A STRING can contain up to 255 characters, not 256. However they use an extra byte (the 0'th) to store their length.

Also the LENGTH keyword only helps when declaring arrays of STRINGs not scalar STRINGs ... though I don't think there is a syntax error reported in the latter case.

Best wishes,

Tom
Edited 2022-08-04 00:44 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2521
Posted: 02:48pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  The PicoMite doesn't have the WORD type.
INTEGER is 64-bit signed (accurate up to 19 decimal digits)
FLOAT is double precision (starts to lose accuracy after 14 decimal digits)
STRING has 256 characters unless you DIM them using LENGTH.


I don't want to use floats so best I define everything as integers??
I've only used integers for timing ie millis function so how are negative integers used?
Can dim dx as integer
dx=-4
work?
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4299
Posted: 02:50pm 03 Aug 2022
Copy link to clipboard 
Print this post

  stanleyella said  Can dim dx as integer
dx=-4
work?


Yes, they are signed 64-bit integers.

You can also do away with that Microsoft DIM ... AS type verbosity and write:

DIM x% = -4


% suffix = integer
! suffix = float
$ suffix = string

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 03:15pm 03 Aug 2022
Copy link to clipboard 
Print this post

I usually use OPTION DEFAULT INTEGER at the beginning then use ! or $ where I need floats or strings respectively. I tend to use more integers than anything else so it's less typing. :) One of the advantages is that you are safe from floating point errors like 4.00000000000001 not being equal to 4.

Even if you don't use OPTION DEFAULT or %, on the PicoMite if you use a=4 it will be treated as a 64 bit integer. If you use a=4.0 or a=4E0 then it will be treated as a float.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2521
Posted: 03:32pm 03 Aug 2022
Copy link to clipboard 
Print this post

@thwill thanks. I wanted a random number between -4 and 4
do
Rand=(rnd*8+1)-4
loop until Rand<>0
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3348
Posted: 03:57pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  Even if you don't use OPTION DEFAULT or %, on the PicoMite if you use a=4 it will be treated as a 64 bit integer. If you use a=4.0 or a=4E0 then it will be treated as a float.


Not so. Undefined variables are float.
> a=4
> a=a/3
> ?a
1.3333333


  stanleyella said  I wanted a random number between -4 and 4


You need to read the manual. Don't guess, don't rely on what you may know from other Basics. And if you post a short snippet of code asking whether it works, why not just try it first? There's little point in asking about a form of a command which doesn't even exist in MMBasic.

See RND.

For a list of what commands and functions are available:

LIST COMMANDS
LIST FUNCTIONS

~
Edited 2022-08-04 02:04 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 05:20pm 03 Aug 2022
Copy link to clipboard 
Print this post

Manual for 5.07.03 page 18

  Quote  
Constants
...
When a constant number is used it will be assumed that it is an integer if a decimal point or exponent is not used. For example, 1234 will be interpreted as an integer while 1234.0 will be interpreted as a floating point number.


If your division had been an integer division then the result would be an integer.


Stanlyella: We aren't going to write this for you. ;)

First, RND will produce a floating point number between 0 and 0.999999. This is normal for most BASICs based on GWBASIC. Consequently you can't directly produce a random number of less than zero.

The traditional way to get a random number between 1 and x is like this:
result=INT(RND*x)+1

I suggest that you play with this formula to derive the range that you need. Write a little test routine to print, say, 10 random numbers to see if it's working.
Edited 2022-08-04 03:37 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10180
Posted: 05:38pm 03 Aug 2022
Copy link to clipboard 
Print this post

Undefined variables are floats unless you use option default integer. Assigning a integer literal does not change this
Edited 2022-08-04 03:41 by matherp
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 05:40pm 03 Aug 2022
Copy link to clipboard 
Print this post

Is the manual wrong then, Peter? The same thing is in the MMBasic Characteristics section.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4032
Posted: 05:45pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  Is the manual wrong then, Peter? The same thing is in the MMBasic Characteristics section.

Er, which same thing? It looks to confirm what Peter posted... What am I missing?

John
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 05:49pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Quote  
CONSTANTS
...
If the decimal constant contains a decimal point or an exponent, it will be treated as a floating point constant; otherwise it will be treated as a 64-bit integer constant.


Just to avoid confusion, this has nothing to do with CONST.
In the statement a = 2 * 1234
a is the variable, 2 and 1234 are constants.
Edited 2022-08-04 03:54 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2521
Posted: 05:55pm 03 Aug 2022
Copy link to clipboard 
Print this post

rnd, yes I forgot int(rnd*x)+1. Last basic rnd was 0 to 255. sinclair and amstrad rnd was 0 to 0.9999.
The idea that vars are integers and memory doesn't matter is new. I'm used to bytes and words and tight code.
A bit more conversion and I'll load and run it but don't want meaningless error messages and this is mmedit which is nicer than teraterm. used to an ide.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 06:01pm 03 Aug 2022
Copy link to clipboard 
Print this post

Default variables can be any type you like. Just set the default at the beginning of your program using:
OPTION DEFAULT INTEGER
OPTION DEFAULT FAULT
OPTION DEFAULT NONE
or whatever you are happy with. :)

Generally the error messages aren't meaningless, but sometimes you have to think about what it's telling you. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4299
Posted: 06:27pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Quote  CONSTANTS
...
If the decimal constant contains a decimal point or an exponent, it will be treated as a floating point constant; otherwise it will be treated as a 64-bit integer constant.


You will have to excuse me if I am misunderstanding the "controversy", but the fact that you are assigning an integer constant to an undeclared variable does not effect its initial type, e.g.



a and c! are floats, b% is an integer.

Best wishes,

Tom
Edited 2022-08-04 04:28 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
pwillard
Guru

Joined: 07/06/2022
Location: United States
Posts: 313
Posted: 06:34pm 03 Aug 2022
Copy link to clipboard 
Print this post

Step back and just imagine you are using a "Sinclair" or "Amstrad" or maybe even an "Apple II" and you just have a better version of Basic and more ram.  I'm pretty sure the Basic in those old home computers only had a few options (like floating-point. integers and strings) for data types and they didn't include "bytes".

And really... you need to isolate your code blocks into things that work before tackling the "whole enchilada" in one bite.
Edited 2022-08-04 04:37 by pwillard
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 06:58pm 03 Aug 2022
Copy link to clipboard 
Print this post

Tom:
In that case the manual is wrong in both places.
Of course, you shouldn't be assigning a number of more than 14 significant digits to a default float variable as it will then (accoring to the manual) be automatically stored as float. Only an integer can handle more than that without error. :)

I'm quietly cursing only having a single monitor. It's a faff having to keep switching it over as it's multiple button presses. :(  I'm not going to test with MMB4W as I've not got it, that's not the manual that I'm looking at and I've no idea if it works the same way.


Edit:

I just tested it and the manual appears to be either wrong or misleading. Simply entering a=1234567890 results in it becoming a float - or at least it does when you print it. Never assume anything. :)
Edited 2022-08-04 05:14 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4299
Posted: 07:15pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  Tom:
In that case the manual is wrong in both places.


Not sure what your second place is, but the section titled "Constants" on p45 seems fine to me ... though personally I would have replaced "Constant" with "Literal" throughout. It's only talking about the type of the literal value it seems to have no impact on the type of the variable you assign the value to ... though of course assigning an integer literal to a float variable or a float literal to an integer variable will perform an appropriate conversion/truncation or report an error.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4299
Posted: 07:16pm 03 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  I just tested it and the manual appears to be either wrong or misleading. Simply entering a=1234567890 results in it becoming a float - or at least it does when you print it. Never assume anything. :)


Do they have a different manual up there in Leyland ? No interpretation I can make of the PicoMite VGA manual would lead me to expect anything else.

EDIT: One of the grognards (look it up, it's not a derogatory term ) may know better but I believe all BASICs are statically typed ... what you are expecting is the behaviour of a dynamically typed language.

Best wishes,

Tom
Edited 2022-08-04 05:46 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2521
Posted: 09:08pm 03 Aug 2022
Copy link to clipboard 
Print this post

The basic I was using is for 8bit microcontrollers. An integer is 16bit like a word but 1 to 32768 is positive and above is negative. So mmbasic seems weird and wasteful of memory for say 16bit numbers if an integer takes 4 bytes.
I thought using floats was slower than whole numbers. I'm not using pi so no need.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 09:09pm 03 Aug 2022
Copy link to clipboard 
Print this post

The two quotes that I've taken about Constants are what's there, in the PicoMite 5.07.03 manual. The page 16 one is quite clear about what to expect:-
  Quote  
Constants
...
When a constant number is used it will be assumed that it is an integer if a decimal point or exponent is not used. For example, 1234 will be interpreted as an integer while 1234.0 will be interpreted as a floating point number.

The second quote, from MMBasic Characteristics in the same manual, verifies it:-
  Quote  
CONSTANTS
...
If the decimal constant contains a decimal point or an exponent, it will be treated as a floating point constant; otherwise it will be treated as a 64-bit integer constant.


If that looks like dynamic typing then I'm not going to argue. :) It's quite possible that it's at least partially correct for one of the other 'Mites, but seems not to apply to the PicoMite.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
     Page 3 of 16    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025