![]() |
Forum Index : Microcontroller and PC projects : dim var as byte
![]() ![]() ![]() ![]() |
|||||
Author | Message | ||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4299 |
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 KingdomPosts: 2521 |
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 KingdomPosts: 4299 |
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 KingdomPosts: 7820 |
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 KingdomPosts: 2521 |
@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 StatesPosts: 3348 |
Not so. Undefined variables are float. > a=4 > a=a/3 > ?a 1.3333333 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 KingdomPosts: 7820 |
Manual for 5.07.03 page 18 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 KingdomPosts: 10180 |
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 KingdomPosts: 7820 |
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 KingdomPosts: 4032 |
Er, which same thing? It looks to confirm what Peter posted... What am I missing? John |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
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 KingdomPosts: 2521 |
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 KingdomPosts: 7820 |
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 KingdomPosts: 4299 |
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 StatesPosts: 313 |
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 KingdomPosts: 7820 |
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 KingdomPosts: 4299 |
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 KingdomPosts: 4299 |
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 ![]() 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 KingdomPosts: 2521 |
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 KingdomPosts: 7820 |
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:- The second quote, from MMBasic Characteristics in the same manual, verifies it:- 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 |
||||
![]() ![]() ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |