Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:28 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 4 of 16    
Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10181
Posted: 09:15pm 03 Aug 2022
Copy link to clipboard 
Print this post

All MMBasic versions are identical in this respect. The quotes relate to the CONST command
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1638
Posted: 09:16pm 03 Aug 2022
Copy link to clipboard 
Print this post

I think constants and variables are being confused

CONST A = 123   'is an integer constant
CONST B = 123.0 ' is a float constant

C = 123 ' is a float variable unless OPTION DEFAULT INTEGER is used.

Bill

EDIT Peter types faster than I do.
Edited 2022-08-04 07:17 by Turbo46
Keep safe. Live long and prosper.
 
Mixtel90

Guru

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

  stanleyella said  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.


You have to remember that the PicoMite isn't an 8-bit microprocessor. All the data paths on the RP2040 are 32 bits wide so attempting to use 8 or 16 bits is still going to use a minimum of 32 bits because of the hardware. A 64=bit integer is only 2 bus accesses. The highest bit is the sign, so you can handle some very big numbers.

You can pack and unpack multiple bytes or 16-bit words into 64 bits if you want to, and write some little routines to handle it.
Mick

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

Guru

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

I tried to read the application to CONST into it, but CONST is introduced later, on page 20, and there is apparently no relationship as the characteristic mentioned isn't shown in that section.
Mick

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

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6266
Posted: 09:43pm 03 Aug 2022
Copy link to clipboard 
Print this post

There are two sections in the manual(s)
The one describing CONST tells us that the CONT being declared will be an integer unless the value being assigned is a float, in which case the CONST will be a float.

The section of the manual headed Constants I think refers to the type use within calculations. Type casting does occur but the starting position for a number is defined by whether it has a decimal or not etc.

Type casting can do strange things.

CONST a = CINT(123 /5)
CONST b = 125 /5
CONST c = INT(123 /5)
CONST d = 125.0 /5
PRINT a
PRINT b
PRINT d

PRINT a/b
PRINT c/b

a is integer
b is integer
c is float
d is ?

Note the difference between CINT and INT

I haven't tested the above in Microchip versions yet.

Jim
VK7JH
MMedit
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1638
Posted: 09:55pm 03 Aug 2022
Copy link to clipboard 
Print this post

I believe that the two sections are talking about the same thing: Constants.

The first section 'Constants' describes how a constant varies from a 'Variable' (which is described in the preceding paragraphs).

The second section 'CONST' describes the use of the CONST command to define a constant.

Bill
Keep safe. Live long and prosper.
 
lizby
Guru

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

Lua is a dynamically typed language. For a=4, a is an integer. If followed by a=a/3, a becomes a floating point variable.

MMBasic doesn't have dynamic typing. In the absence of setting the type of "a", for a=4, "a" is a floating point variable which happens to have a value which is an integer.

dim integer i=4
a=4
if i=a then ?"Equal"  ' prints "Equal"

Or

dim integer i=4
a=4
i=i/3
a=a/4
?i,a ' prints 1 1.33333333

4 is a literal which has an integer value; 1.3 is a literal which has a floating point value. Assigning either literal to a variable doesn't change the variable type.

The manual doesn't say anything that contradicts this.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

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

  stanleyella said  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.


Unless you are running out of variable space, it's neither weird nor wasteful to store integers as 64-bit numbers. I understand where you are coming from, since I came from the same place, and in particular it seemed painful for 1-bit flags to occupy 64 bits.

CaptnBoing felt the same way, and in fruitoftheshed.com, he has routines for packing and unpacking multiple flags into a single word--but as he himself has said, what you gain in memory efficiency, you lose in running time.

These days, it's almost always the case that the scarce resource is programmer time, not memory and not execution speed (there are exceptions, of course). MMBasic is great for enabling the minimization of programmer time in implementations which provide adequate speed and adequate memory for most of what hobby programmers need (and also for what some who program for a living need).
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2579
Posted: 11:13pm 03 Aug 2022
Copy link to clipboard 
Print this post

I have some time to waste so I will stick my oar in, for what little it may be worth.

Part of the confusion about what the manual means may be due to the interpretation of "="
In normal arithmetic it means "is equal to" but not in Basic.
In most programming languages, when use in a statement, it means "The value of variable to the left of = is replaced by an evaluation of all that is to the right"
In other situations it is for comparing expressions etc.

In
A = 4
If A's type has not been fixed previously the integer constant 4 will be evaluated as a float when it replaces the current value of A in most Basics that I have used.
Edited 2022-08-04 09:14 by phil99
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6266
Posted: 11:13pm 03 Aug 2022
Copy link to clipboard 
Print this post

Ignore my earlier ramblings.
OPTION DEFAULT FLOAT
CONST a = &hFFFFFFFFFFFFFFFF
CONST aa = -1.0
PRINT a
b = a
c% = a
PRINT b
PRINT c%
PRINT "a"
FOR n = 0 TO 7
PRINT HEX$(PEEK(VAR a,n),2);
NEXT n
PRINT
PRINT "aa"
FOR n = 0 TO 7
PRINT HEX$(PEEK(VAR aa,n),2);
NEXT n
PRINT
PRINT "b"
FOR n = 0 TO 7
PRINT HEX$(PEEK(VAR b,n),2);
NEXT n
PRINT
PRINT "c"
FOR n = 0 TO 7
PRINT HEX$(PEEK(VAR c%,n),2);
NEXT n
PRINT


RUN
-1
-1
-1
a
FFFFFFFFFFFFFFFF
aa
000000000000F0BF
b
000000000000F0BF
c
FFFFFFFFFFFFFFFF
>


Note that the bytes are in reverse order. It still shows that CONST types depend on what you assign to them and do NOT follow the OPTION DEFAULT declaration.

Jim
Edited 2022-08-04 10:41 by TassyJim
VK7JH
MMedit
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3348
Posted: 01:34am 04 Aug 2022
Copy link to clipboard 
Print this post

  lizby said  Or

dim integer i=4
a=4
i=i/3
a=a/4
?i,a ' prints 1 1.33333333


That should have been "a=a/3" instead of "a=a/4".
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4032
Posted: 07:01am 04 Aug 2022
Copy link to clipboard 
Print this post

  phil99 said  In
A = 4
If A's type has not been fixed previously the integer constant 4 will be evaluated as a float when it replaces the current value of A in most Basics that I have used.

I would rephrase that as:

In
A = 4
If A's type has not been fixed previously (and there's no OPTION DEFAULT INTEGER) then it (i.e. the variable A) will be float.

The integer constant 4 will be stored in A.  (This won't alter A being a float.)

---

If you PRINT A it may confuse you! Don't be confused - the above is still true.

---

CONST doesn't alter the above. A would still be a float if you assigned it using a CONST item (literal, in Tom's post).

---

Also, because 4 can be represented (in the internal way used by the CPU) exactly as a float it will compare as equal in things like:

IF A = 4 THEN ...

That's because storing 4 into a float doesn't need any rounding etc. It fits fine but doesn't alter A being a float (once a variable has a type it keeps it).

John
Edited 2022-08-04 17:11 by JohnS
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2579
Posted: 07:22am 04 Aug 2022
Copy link to clipboard 
Print this post

" (and there's no OPTION DEFAULT INTEGER) "

I would regard that as "has been fixed previously" since all variables are integers unless you specify otherwise in that case.

Your other point is right, when "=" is used for a comparison an integer can be the same as a float. It's the value that is being compared not the type.

While I am here I would like to expand another line from:-
"The value of variable to the left of = is replaced by an evaluation of all that is to the right"
to:-
"The value of the variable to the left of = is replaced by an evaluation of all that is to the right, according to the rules of the language"
Edited 2022-08-04 17:33 by phil99
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 07:31am 04 Aug 2022
Copy link to clipboard 
Print this post

IMHO that's the best description so far, John. :)

Perhaps all that's required to remove confusion is to add an additional sentence to the first manual reference:
  Quote  
Constants
These are created using the CONST command. Numeric constants may begin ...


The later reference to CONST can then be left as it is.
Mick

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

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1638
Posted: 07:32am 04 Aug 2022
Copy link to clipboard 
Print this post

  Quote  A = 4
If A's type has not been fixed previously the integer constant 4 will be evaluated as a float when it replaces the current value of A in most Basics that I have used.


Sorry to labour the point but:
A = 4
does not define a constant.
you can follow that with:
A = A + 1

BUT
CONST A = 4
Does defines a constant it cannot be changed
in this case A is an integer.
CONST B = 5.0 is a float.

Please RTFM.

Bill
Keep safe. Live long and prosper.
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1638
Posted: 07:35am 04 Aug 2022
Copy link to clipboard 
Print this post

   the manual said  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


Bill
Keep safe. Live long and prosper.
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2579
Posted: 07:44am 04 Aug 2022
Copy link to clipboard 
Print this post

My power to explain things clearly is inadequate.

in A = 4 I should have said it is the number 4 that is the constant, it is not a variable so cannot be anything else. A is the variable and it's type is determined by the rules of the language, unless previously set.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7820
Posted: 08:00am 04 Aug 2022
Copy link to clipboard 
Print this post

For a CONST command the "=" sign is an assignment operator that assigns both the value *and type* of the expression to the "carrier". It ignores any previous assignment such as OPTION DEFAULT. The "carrier" isn't a variable - it just happens to look like one.  :)  The "carrier" then carries it's value and type so that it can be used elsewhere.
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: 08:54am 04 Aug 2022
Copy link to clipboard 
Print this post

Thanks folks,

Regarding the type of CONST "variables", that is an interesting quirk of which I was unaware, though not relevant to me since I am a religious user of type suffixes on identifiers.

Regarding the documentation, aren't we lucky to have such great documentation that we can pick nits with .

Have a great day,

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: 10:48am 04 Aug 2022
Copy link to clipboard 
Print this post

TBH I feel a bit rotten about the nit-picking as the manual is excellent. :(  In this particular case I found it confusing though.
Mick

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