Background
The cross-compiler cc65 has one big disadvantage: there is no floating-point arithmetic implemented. In other words, neither the data type ‚float‘ has been implemented nor the format specifier ‚%f‘ works. With knowing that, it is clear that floating-point numbers in double length are also not implemented.
As a result, programs compiled with cc65 can only use signed and unsigned integers (like in -2, -1, 0, 1, 2…) up to a width of 32 bits. A floating-point number like 3.14 is not supported. This is a major roadblock for even simple programs in which the fractional part of an integer is important, e.g. when calculating with the constant Pi (~3.14159).
How it works
I wrote a small library that takes two floating-point numbers as strings and converts them to a Q16.16 fixed format in ARM Q notation. Q16.16 means that the floating-point variable is represented by 16-1 bit for the integer part and 16 bit for the fractional part. ARM Q notation means that the very first bit is the sign (+/0 or -/1) and part of the notation. The converted, fixed numbers can now be added, subtracted, and multiplied. The result returned by the library is again a fixed Q16.16 integer that can be converted to a string for printing. „Fixed“ means that the period „.“ separating the integer from the fractional part always stays at the same place, with Q16.16 in the middle:
0000 0000 0000 0011 . 0000 0000 0000 0000
Above bitstring is the positive 3.0: The first bit from the left is 0 (stands for a positive number), the remaining bits on the left side of the period are binary 11 (equals to decimal 3… 1*2^1+1*2^0). The fractional part right of the period is zero.
By the way, this is not exactly the same as the IEEE 754 standard for representing single-precision floating-point numbers in 32 bits. This standard presents the number as a sign bit plus an exponent plus a mantissa (fractional part), comparable to a Q9.23 format.
Let’s take a look at how Q16.16 works in detail. We assume that the two input strings are
„42.1“, „2.0“
Both strings are being converted to two fixed, 32-bit long integers in Q16.16 format. First step:
42 = 0000 0000 0010 1010 + .1
2 = 0000 0000 0000 0010 + .0
The integers 42 and 2 must be moved left of the period. As we are dealing with 32-bit numbers, this means multiplying with 65536:
| Input, to be moved left | Binary, after multiplication with 65536 | Decimal |
| 42 | 0000 0000 0010 1010 . 0000 0000 0000 0000 | 2752512 |
| 2 | 0000 0000 0000 0010 . 0000 0000 0000 0000 | 131072 |
The first integer 42 has a fractional part „.1“. The 1 stands for a 1/10 of a decimal 1, so the scaling factor is 10. Now let’s multiply 1 with 65536 / 10 , which results in 0001 1001 1001 1010 in binary form. Finally, we can add both numbers to get our first fixed number 42.1:
0000 0000 0010 1010 . 0000 0000 0000 0000
+ 0001 1001 1001 1010
--------------------------------------------
= 0000 0000 0010 1010 . 0001 1001 1001 1010
Above result is decimal 1382810. Now for the second number, 2:
0000 0000 0000 0010 . 0000 0000 0000 0000 = 131072
Note that 131072 is 2x 65536. 65536 is the maximum number we can have right of the period, 32767 is the maximum number we can have left of the period (minus the first bit, as it it used as the sign).
As the fractional part of 2.0 is zero, nothing else is to do. Multiplying both numbers results in
0000 0000 0010 1010 . 0001 1001 1001 1010* 0000 0000 0000 0010 . 0000 0000 0000 0000-------------------------------------------
= 0000 0000 0101 0100 . 0011 0011 0011 0100
We convert the result into two decimal values by adding the left part to the right part:
0000 0000 0101 0100 = 84
+ 0011 0011 0011 0100 = 13108 / 65536 = 2.000012...-------------------------------------------
= 84.2000 (rounded)
This is exactly the result we expected!
How to test the library
Write a small C program that includes the header file for my float library:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "float.h"
Now take two numbers as input into two strings input1 and input2:
int main(void) {
char input1[33], input2[33], output[33]; // 32 bit plus '\0'
int32_t fixed1, fixed2, fixed_result;
printf("Enter first floating-point number: ");
if (!fgets(input1, sizeof(input1), stdin)) {
printf("Error: no input!\n");
return(1);
}
printf("Enter second floating-point number: ");
if (!fgets(input2, sizeof(input2), stdin)) {
printf("Error: no input!\n");
return(1);
}
Take both input strings input1 and input2, convert them to fixed Q16.16 integer format using the function to_fixed_int( ), do the arithmetic, and print the result as a string:
fixed1 = to_fixed_int(input1);
fixed2 = to_fixed_int(input2);
printf("fixed1: %b\n", fixed1);
printf("fixed2: %b\n", fixed2);
fixed_result = fixed_mul(fixed1, fixed2);
fixed_to_str(fixed_result, output, sizeof(output));
printf("result: %b\n", fixed_result);
printf("%s\n", output);
return(0);
}
Above example multiplies input1 with input2 using the fixed_mul( ) function in my library. Other arithmetic functions can be found in the header file float.h. The function fixed_to_str( ) takes the result in fixed Q16.16 integer format and converts it to string.
Entering 42.1 and 2.0 in above program, the printed result on stdout must be
84.2000
How to use the library
Before the compiled C program is moved over from the development platform to the Commodore 64, the code can be quickly tested e.g. on a X86 platform with a GCC compiler:
$ gcc -c float.c
$ gcc float.o yourcode yourcode.c
where float.c is my floating-point library and yourcode.c is your C software. Now start your compiled software with
$ ./yourcode
If everything is working fine, use cc65 to compile the C code for the MOS 6502 platform:
$ cl65 -c float.c
$ cl65 -o yourcode.prg float.o --cpu 6502 -t c64 yourcode.c
The result is the yourcode.prg file, which is an executable binary file for the Commodore 64. Move it over to the Commodore 64 and start it:
load"yourcode",8,1
run
The float library for cc65 can be found on GitHub under https://github.com/jklingel/float.
Addendum
I saw on the cc65 mailing list that some people requested float support for cc65, but as of 11/22/2026 nobody succeeded in implementing this natively in the compiler. As my C knowledge is not good enough to suggest expansions to the cc65 source code, I took the easy route and wrote a stand-alone library.
The only other approach to implementing floating-point arithmetic I found so far is cc65-floatlib, but here the author followed a completely different path of using C64 kernal routines. His code was a bit too complicated for me as a beginner in C. The floating-point functions of the C64 ROM are not IEEE 754 compatible, using 5 bytes (40 bits) to represent the number. From BASIC, this can be accessed easily with i.e.
READY
PRINT 3.1 + 3.2
6.3
The disadvantage of cc65-floatlib in my opinion is that the code depends on the Commodore’s zero page and other hardware-related functions. One could not compile, link, and test the code on another platform like X86 and only move the binary over to the C64 when everything is working. This works perfectly with my approach.
Yet another approach is Berkeley SoftFloat. Berkeley SoftFloat is a complete software implementation of binary floating‑point operations that conforms to the IEEE Standard for Floating‑Point Arithmetic. It is similar to my approach in that it can be linked to your existing C code. Unfortunately, I neither understood the test code nor was the library working for me.