Bug in QL C - Lattice C?

Anything QL Software or Programming Related.
Post Reply
ohcan_ognara
ROM Dongle
Posts: 3
Joined: Thu Nov 03, 2016 4:10 pm

Bug in QL C - Lattice C?

Post by ohcan_ognara »

Hello

After 30 years or more I've come back to QL playing with ancient QLC. I'm using the POW() function but it seems I've never been able to obtain correct results. This little program below returns 1.000000 whereas it should return 4.000000.

Is that a bug or do you thing I'm doing something wrong...

Thanks

Code: Select all

#include <mdv1_stdio_h>
#include <mdv1_math_h>

main()
{

	double ov2;

	ov2 = pow(2,2);
	printf("%f\n", ov2);

}


User avatar
XorA
Site Admin
Posts: 1358
Joined: Thu Jun 02, 2011 11:31 am
Location: Shotts, North Lanarkshire, Scotland, UK

Re: Bug in QL C - Lattice C?

Post by XorA »

try pow(2.0,2.0)

pow takes doubles as arguments and you've passed it ints, older compilers don't always promote correctly.


User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Bug in QL C - Lattice C?

Post by NormanDunbar »

What do you get if you do this instead?

Code: Select all

   ov2 = pow(2.0, 2.0);
Or

Code: Select all

   ov2 = pow((double)2.0, (double)2.0);

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
ohcan_ognara
ROM Dongle
Posts: 3
Joined: Thu Nov 03, 2016 4:10 pm

Re: Bug in QL C - Lattice C?

Post by ohcan_ognara »

Thanks Norm and XorA.

Indeed both solutions work. I was a little confused since the very same code worked on Lattice 3.04 (Atari ST). It seems as you stated correctly that Lattice 3.02 (QL) is not able to promote ints to doubles correctly.

Thank you guys!


User avatar
ql_freak
Gold Card
Posts: 353
Joined: Sun Jan 18, 2015 1:29 am

Re: Bug in QL C - Lattice C?

Post by ql_freak »

Warning: QLC (Lattice C 3.02) has one really horrible bug in stdio.h. When it returns EOF (which must be an int - that was always so, even in K&R C), it returns a char. As char is signed on the 68000, when the character code FF (decimal 255) exists in a file, it is sign extended and EOF (-1) is returned. This makes the stdio functions nearly unusable for binary data. BTW: The first versions of C68 have had the same bug.

Correct behaviour is: Return 0xFF (255) as an int (0x00FF) for the character code 0xFF and -1 as int (0xFFFF) for EOF.

BTW: For your example you should at least get a warning if you change it to:

Code: Select all

#include <mdv1_stdio_h>
#include <mdv1_math_h>

double pow(double, double); /* Lattice C supports function prototypes, albeit not ANSI-C compatible */

main()
{

   double ov2;

   ov2 = pow(2,2);
   printf("%f\n", ov2);

}


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
Post Reply