printf is spitting out random decimal points

Questions about the BASICtools and MakeItC
Post Reply
Mr_Simplicity
Posts: 8
Joined: Sun Feb 17, 2013 5:05 pm

printf is spitting out random decimal points

Post by Mr_Simplicity »

This code:
main:
PRINT "Starting..."
WHILE (1)
For x = 0.0 to 10.0 step 0.100
PRINTF ("%1.4f", x)
WAIT(100)
Next x
LOOP
END

Outputs:
Welcome to ARMbasic Kernel[8.12] with Floating Point Copyright 2012, Coridium Corp.
for the SuperPro

Starting...
0.0000
. 0.1000
. 0.2000
. 0.3000
. 0.4000
. 0.5000
. 0.6000
. 0.7000
. 0.8000
. 0.9000
. 1.0000
. 1.1000
. 1.2000
. 1.3000
. 1.4000
. 1.5000
. 1.6000
. 1.7000
. 1.8000
. 1.9000
. 2.0000
. 2.1000
. 2.2000
. 2.3000
. 2.4000
. 2..5000
. 2.6000
. 2.7000

Any ideas?



basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: printf is spitting out random decimal points

Post by basicchip »

I'm kind of surprised it works at all, but I guess the compiler should maybe warn, though the code is not quite wrong.

The only variables that are auto-declared are integers, so x would be an integer, so again surprised the for loop even ended. So far had not tried to write FOR loops with floating point value, and my first test shows it does work, sometimes we luck out.

So this worked for me

Code: Select all

dim x as single
for x=0.1 to 1.0 step 0.1
    print x
    printf("%1.6f",x)
next x

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: printf is spitting out random decimal points

Post by basicchip »

PS, like C the compiler does not do any type of checking for parameters passed in a printf.

So if you apply a format of a SINGLE to a variable INTEGER, you will get maybe what you didn't expect, GIGO -- garbage in garbage out.

Mr_Simplicity
Posts: 8
Joined: Sun Feb 17, 2013 5:05 pm

Re: printf is spitting out random decimal points

Post by Mr_Simplicity »

Yea. I did actually declare x a SINGLE just didn't show all of the program.
It turns out to be the Timer Interrupt that I also had running that causes the extra decimal points, one per interrupt.
I'm not printing these decimal points.
Whats going on? I don't know.

Here's all the code.

Code: Select all

#include "LPC17xx.bas"
#include "PULSE.bas"
#include "HWPWM17.bas"
#include "STRING.bas"

DIM		x				AS	SINGLE							' 
DIM		Every_x_ms		AS	INTEGER							' 

SUB ON_TIMER ( msec, dothis )								' 
	TIMER1_ISR	= dothis + 1								' set function of VIC
	VICIntEnable = VICIntEnable or (1<<2)					' Enable interrupt
	T1_MR0	= msec-1 										' set up match number of ms
	T1_MCR	= 3												' Interrupt and Reset on MR0
	T1_IR	= 1												' clear interrupt
	T1_TC	= 0												' clear timer counter
	T1_TCR	= 1												' TIMER1 Enable
ENDSUB														' 


INTERRUPT SUB DO_PID										' 
	T1_IR = 1												' Clear interrupt
ENDSUB														' 


main:
	FIO2DIR 		=	FIO2DIR OR (1<<0)					' Make PWM1 (Pin 2.0) an Output...
	P2(0)			=	0									' 	and set it low (Pump Full ON)
	Every_x_ms		=	1000
	
	
	ON_TIMER(Every_x_ms, ADDRESSOF DO_PID)					' Control Pressure every 10ms (100/Sec)

	PRINT "Starting..."
	
	WHILE (1)
		For x = 0.0 to 10.0 step 0.100
			PRINTF(" %1.6f",x)  
			WAIT(100)
		Next x
	LOOP
	
END

Post Reply