Alternative STR$ for small value numbers

Basic and Machine Language

Moderator: Moderators

Post Reply
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Alternative STR$ for small value numbers

Post by wimoos »

Printing out numbers smaller than 0.01 is done in exponential notation. Which is generally fine.
However, sometimes it preferable to have small numbers printed in natural notation.
The following (relocatable) code does that.
Because a significant decimal is lost in the process, numbers smaller than 1e-8 are rounded down to 0.
Set the USR() vector to the start of the code and have USR() work as an alternative to STR$().

Example:

Code: Select all

PRINT STR$(1E-4)
 1E-04

PRINT USR(1E-4)
 .0001

Regards,

Wim.

Code: Select all

	jsr $cd8d	; check type numeric
	lda $61
	cmp #$7b	; between -0.015625 and 0.015625 ?
	bcs l500	; branch if large enough
	cmp #$66	; between -1e-8 and 1e-8
	bcc l200	; set it to zero
	lda $66		; get sign
	pha		; save sign
	lsr $66		; set sign to positive
	lda #$bc
	ldy #$d9
	jsr $d867     	; add 1.0
	pla		; retrieve sign
	sta $66		; restore it
	jsr $dddf	; perform STR$
l300	iny
	lda $00ff,y	; one position to the left
	sta $00fe,y
	bne l300
	jmp $d46d	; exit
l200	lda #$00
	sta $61
l500	jmp $d468
Last edited by wimoos on Thu Apr 26, 2018 3:46 am, edited 1 time in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
CurtisP
Vic 20 Dabbler
Posts: 99
Joined: Tue Mar 08, 2005 8:24 pm

Re: Alternative STR$ for small value numbers

Post by CurtisP »

It still blows my mind a little that USR can return a string value.
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Alternative STR$ for small value numbers

Post by wimoos »

There is not much to it. The dollar sign is just part of the token: this doesn't impose anything.

The underlying routine puts the stringdescriptor on the stack of temporary string descriptors, sets the datatype to "string" and then skips datatype checking (which is against "numeric"). The caller (PRINT, LET or any other function that takes a string) just pulls the descriptor off the stack when it needs it.Thats it.

Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
Post Reply