1

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 */
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Are you compiling a program for userland or a kernel module? Can you print the value of `sizeof(long double)`? It should be 12 bytes. Also are you running on an old 3B2 box or an intel CPU? – chqrlie May 03 '20 at 19:56
  • Another hint: do you link your program with `-lm` to add floating point support for `printf`? – chqrlie May 03 '20 at 20:03
  • More information about Xinu: https://xinu.cs.purdue.edu/ What does "Function `sqrtl` doesn't work here" mean? If you got an error message, please include it in your question. You have mismatched parentheses in your program. Please copy-and-paste your actual code. – Keith Thompson May 03 '20 at 20:52
  • 1
    With a conforming implementation, `printf("%Lf\n", sqrtn)` should work correctly. There are probably some things that the Xinu implementation doesn't support. – Keith Thompson May 03 '20 at 20:53
  • 1) To investigate such problems, avoid `"%f"` and friends, use `"%g"`. 2) report data entered and exact output seen. – chux - Reinstate Monica May 05 '22 at 02:56

2 Answers2

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • *write your own code to convert a `long double` value to a string*... Not for the faint of heart. So ride, boldly ride, to the end of the rainbow, Ride, boldly ride, till you find El Dorado – chqrlie May 03 '20 at 22:05
  • @chqrlie Writing your own code could include copying an open source implementation. – Keith Thompson May 03 '20 at 23:56
-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.