'How to print long double number in xinu?

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 */


Solution 1:[1]

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.

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Göran Häggsjö