How to print long double number in xinu?

210 views Asked by At

Function sqrtl doesn't work here, also the printf of long double prints f for all given numbers:

#include <conf.h>
#include <kernel.h>
#include <math.h>
#include <float.h>

long double sqrtn; 
unsigned long int n;    

void xmain() {
    unsigned long int n;   
    printf("Enter unsigned long integer:\n"); 
    scanf("%lu", &n); 
    sqrtn = sqrtl(long double)n);
    printf("\nsqrtn=%Lf\n", sqrtn); 
} /* main */
2

There are 2 answers

2
Keith Thompson On

I downloaded the Xinu source code for x86 from https://xinu.cs.purdue.edu/files/Xinu-code-Galileo.tar.gz .

Looking at the implementation for printf (lib/printf.c lib/doprnt.c), as far as I can tell it simply doesn't support length modifiers. That means that, for example, this:

long int n = 42;
printf("%ld\n", n);

wouldn't work. I suggest trying that on your system.

This is not a conforming C implementation (and it's probably not intended to be).

It does appear to support most of the standard conversion specifiers ("%d", "%u", "%x", "%f", etc.).

If you want to print a long double value, I think the best you can do is either convert it to double and use "%f" (which could lose range and/or precision) or write your own code to convert a long double value to a string. (Or run you code on a different system).

Disclaimer: I haven't tried this, I've only examined the source code, and only for the x86 version of the system.

1
Göran Häggsjö On

I was worried about long double won't print. i wrote a small test and found out that my MAC wanted to have %Lf or %Le. it seems that capital L must be used.