Trouble with floats

Discussion about other targets
Post Reply
bert
New member
Posts: 2
Joined: Tue Oct 16, 2018 6:46 pm

Trouble with floats

Post by bert »

Hi all!

I'm new to z88dk, and I try to compile a c program written for the sdcc.
The program uses a multiplication between int and float which can be done on compile time. In sdcc it works, but with zcc I got zero as result.
Here is the code:

Code: Select all

#include <stdio.h>
#include <stdint.h>

#define WIDTH  (40)

void do_something( uint8_t param1, uint8_t param2)
{
    printf( "param1: %u\n", param1);
    printf( "param2: %u\n", param2);
}

void main()  
{
    printf( "expect: 18, 20\n");
    do_something( WIDTH * 0.45, WIDTH * 0.5);
}
This is the output:
expect: 18, 20
param1: 0
param2: 0
Just for completness the compiler invocation:
zcc +kc85 -create-app --list --c-code-in-asm -lmath48 -lndos -o float_test main.c
Does anybody have a hint, what I'm doing wrong?

Best regards,
Bert
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

It is a bug in sccz80; the compiler is generating 0.0 for the two constants "WIDTH * 0.45" and "WIDTH * 0.5".
It works with zsdcc as compiler. I'll post the issue number shortly.
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

bert
New member
Posts: 2
Joined: Tue Oct 16, 2018 6:46 pm

Post by bert »

Great!
After the bugfix the program work as expected.

Best regards,
Bert
User avatar
RobertK
Well known member
Posts: 347
Joined: Mon Feb 26, 2018 12:58 pm

Post by RobertK »

I've noticed another problem with floats on the KC target, it could be a console problem. When you printf() a floating point value, the screen gets broken with vertical stripes.

Code: Select all

// floatcalctest.c - Floating point calculation test
// Author: RobertK, 2018-12-29

// Compile with:
// zcc +gal -create-app -lm -o fct floatcalctest.c
// zcc +zx81 -create-app -lm -startup=2 -o fct_zx81 floatcalctest.c
// zcc +zx80 -create-app -lm -o fct_zx80 floatcalctest.c
// zcc +kc -lndos -lm -create-app floatcalctest.c -o fct_KC85_2-5

#include <stdio.h>

void main()
{   
  printf("\nhello world\n");
  printf("\npress any key to calculate 5*0.1...\n");
  fgetc_cons();        // wait for keypress          
  printf("\n5*0.1 is %.1f\n",5*0.1);
  printf("\npress any key to exit...\n");  
  fgetc_cons();        // wait for keypress          
}
Compile it like this:

Code: Select all

zcc +kc -lndos -lm -create-app floatcalctest.c -o fct_KC85_2-5
Try it in JKCEMU with the system set to e.g. KC85/5.

BTW, using -lmath48 instead of -lm makes no difference.
User avatar
dom
Well known member
Posts: 2091
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

Thanks Robert, I'm guessing this is another index register problem - the port is switched over to use iy rather than ix, but I think the maths libraries aren't.

I'll create iy flavours of the maths libraries and see if that solves the problem.
User avatar
dom
Well known member
Posts: 2091
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

I've switched +kc to an ix/iy swapped version of the maths library and the issue has gone away. This makes sense since the main C library for +kc is also ix/iy swapped.

It looks like the kc goes a bit wrong on exit regardless of what you do, but that's a smaller issue.

BTW, the calculation actually takes place in the compiler in this case...
stefano
Well known member
Posts: 2151
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

well done ! :)
Post Reply