1

I have a task to print a float number using write() function without using any standard library functions like sprintf etc ...

void    putnbr(int number)
{
    if (number >= 0 && number <= 9)
    {
        number += 48;
        write(1, &number, 1);
    } else if (number > 9)
    {
        putnbr(number / 10);
        putnbr(number % 10);
    } else if (number < 0)
    {
        write(1, "-", 1);
        putnbr(number * (-1));
    }
}

this function works for int numbers but it's not working for float numbers

  • 2
    You need to write a new function that takes a `double` argument instead of an `int` argument. And of course, your new function will need to handle a wider range, fractional digits. etc. – Tom Karzes Sep 20 '21 at 01:00
  • 2
    For floating point numbers, you will also be emitting digits. The challenge is that at some time you might have to write a dot or comma. So you need to think of how a floating point number is stored in memory, and just build out the logic. Since you have gotten the task, you are expected to have the appropriate knowledge. So give it a try, and when you run into a specific problem, post the code that you have and ask the specific question. Because this is not a code writing service. – Cheatah Sep 20 '21 at 01:02
  • I know that but what I don't know is how to convert that double number to string because as you know write() function only accepts a string – Hssain Aitkadir Sep 20 '21 at 01:03
  • 1
    Make an override of your putnbr function that receives a float/double as a parameter. Then you separate the left side of the decimal delimiter and use the existing putnbr function on that (divide by one and cast into int) then write the decimal delimiter and at the end take the things behind the delimiter... You can subtract the previously calculated "left of decimal delimiter" value and multiply with how many decimal places you want to write (idk? 5? So multiply by 10000) cast to int and use your function on that – DaMachk Sep 20 '21 at 01:10
  • Consider the number 123.456. Cast it to `int` (discarding the fraction): 123. That's an integer, you know how to print it. Now subtract 123.456 - 123 = 0.456. That's the fraction you have to print. Multiply it by 10: 4.56. Cast it to `int`: 4. That's the first digit after the decimal you have to print. Now subtract 4.56 - 4 = 0.56. Multiply it by 10: 5.6. Cast it to `int` to get the next digit past the decimal to print. Repeat until the subtraction gives too small a number. (Note that it will almost never go to 0.) This is a crude, brute-force algorithm, but it's a start. – Steve Summit Sep 20 '21 at 01:19
  • 3
    never use magic numbers like 48. Use `'0'` instead – phuclv Sep 20 '21 at 01:32
  • @SteveSummit thanks for your useful information I really appreciate I'll try to convert your algorithm to code – Hssain Aitkadir Sep 20 '21 at 01:45
  • To print a `float` _exactly_ to a _string_ take a bit of code: [Function to print a double - exactly](https://codereview.stackexchange.com/q/212490/29485). For a learner, an _approximate_ result should be OK, yet OP's goal lacks detail. – chux - Reinstate Monica Sep 20 '21 at 04:09
  • okadir, casts to an `int` or even `long long` fails for large values. – chux - Reinstate Monica Sep 20 '21 at 04:15
  • 5 min google search of `float to string in C` : https://www.geeksforgeeks.org/convert-floating-point-number-string/ – KamilCuk Sep 20 '21 at 08:20
  • 1
    @KamilCuk [Linked code](https://www.geeksforgeeks.org/convert-floating-point-number-string/) has `float n .... int ipart = (int)n;` which fails when `n` much outside `int range. – chux - Reinstate Monica Sep 20 '21 at 15:09

1 Answers1

0

Hi you could do it like that :

include <unistd.h>

void printchar(char c)
{
    write(1, &c, 1);
}

int put_nbr(int nb)
{
    if ( nb > 9)
        put_nbr(nb / 10);
    else if (nb < 0) {
        nb = nb * -1;
        write(1, "-", 1);
        put_nbr(nb/10);
    }
    printchar(nb % 10 + '0');
}

it should be clear enough but tell me if you need more precision

Tsirsuna
  • 130
  • 2
  • 16