print float and double in minix - Minix

This is a discussion on print float and double in minix - Minix ; I experienced some probleme with the float and double when I print them. in a nutshell if i write float temp = 1.9 printf(temp %f, temp); in the stdout apper temp %f...

+ Reply to Thread
Results 1 to 9 of 9

Thread: print float and double in minix

  1. print float and double in minix

    I experienced some probleme with the float and double when I print
    them.
    in a nutshell if i write

    float temp = 1.9
    printf(temp %f, temp);
    in the stdout apper temp %f

  2. Re: print float and double in minix

    On 06/27/08 22:30, puntino wrote:

    > I experienced some probleme with the float and double when I print
    > them. in a nutshell if i write


    Is the following code from a C program or for a shell command?

    > float temp = 1.9
    > printf(temp %f, temp);
    > in the stdout apper temp %f


    For a C program try with:
    printf("temp: %f\n", temp);

    For a shell command try:
    printf "temp: %f\n" 1.9

    Ciao
    Giovanni
    --
    A computer is like an air conditioner,
    it stops working when you open Windows.
    Registered Linux user #337974 < http://giovanni.homelinux.net/ >

  3. Re: print float and double in minix

    On Jun 28, 11:33 am, Giovanni wrote:
    > On 06/27/08 22:30, puntino wrote:
    >
    > > I experienced some probleme with the float and double when I print
    > > them. in a nutshell if i write

    >
    > Is the following code from a C program or for a shell command?
    >
    > > float temp = 1.9
    > > printf(temp %f, temp);
    > > in the stdout apper temp %f

    >
    > For a C program try with:
    > printf("temp: %f\n", temp);
    >
    > For a shell command try:
    > printf "temp: %f\n" 1.9
    >
    > Ciao
    > Giovanni
    > --
    > A computer is like an air conditioner,
    > it stops working when you open Windows.
    > Registered Linux user #337974


    Thank you Giovanni,

    I don't need to try anymore because simply it is not possible for my C
    program.
    My program is a server and for them, I've learnt, that the "printf"
    statament is define as "kprintf"
    If you look at sys/kprintf.c file you will not find any "case" for the
    "f" formatting
    character like in a "standard" printf .

  4. Re: print float and double in minix

    puntino wrote:
    > On Jun 28, 11:33 am, Giovanni wrote:
    > Thank you Giovanni,


    > I don't need to try anymore because simply it is not possible for my C
    > program.
    > My program is a server and for them, I've learnt, that the "printf"
    > statament is define as "kprintf"
    > If you look at sys/kprintf.c file you will not find any "case" for the
    > "f" formatting
    > character like in a "standard" printf .


    That's because, in general, you don't really want to use floats in
    kernel or server code because those operations are both slow and
    inherently imprecise. What exactly is the reason you are using floats?

    Regards,

    Jens

    --
    Jens de Smit
    Student Computer Science | Vrije Universiteit Amsterdam
    jfdsmit@few.vu.nl | http://www.few.vu.nl/~jfdsmit
    "[In the end, people] get furious at IT that the goddamn magic isn't working"
    -- Stewart Dean

  5. Re: print float and double in minix

    On Jun 30, 11:38 am, "J.F. de Smit" wrote:
    > puntino wrote:
    > > On Jun 28, 11:33 am, Giovanni wrote:
    > > Thank you Giovanni,
    > > I don't need to try anymore because simply it is not possible for my C
    > > program.
    > > My program is a server and for them, I've learnt, that the "printf"
    > > statament is define as "kprintf"
    > > If you look at sys/kprintf.c file you will not find any "case" for the
    > > "f" formatting
    > > character like in a "standard" printf .

    >

    I wanted my server to do some operations like to calculate average
    time and so on..




    That's because, in general, you don't really want to use floats in
    > kernel or server code because those operations are both slow and
    > inherently imprecise. What exactly is the reason you are using floats?
    >
    > Regards,
    >
    > Jens
    >
    > --
    > Jens de Smit
    > Student Computer Science | Vrije Universiteit Amsterdam
    > jfds...@few.vu.nl |http://www.few.vu.nl/~jfdsmit
    > "[In the end, people] get furious at IT that the goddamn magic isn't working"
    > -- Stewart Dean



  6. Re: print float and double in minix

    puntino wrote:
    > I wanted my server to do some operations like to calculate average
    > time and so on..


    What you could do is manually format the output: convert float f to int
    i1, deduct i1 from f, multiply f by 100 (for 2 decimal digits) and convert
    f to another int i2. You can then use

    printf("%d.%02d", i1, i2);

    to get your desired output (the 02d makes sure that the decimal digits are
    zero-padded). Good luck!

    Regards,

    Jens


    --
    Jens de Smit
    Student Computer Science | Vrije Universiteit Amsterdam
    jfdsmit@few.vu.nl | http://www.few.vu.nl/~jfdsmit
    "[In the end, people] get furious at IT that the goddamn magic isn't working"
    -- Stewart Dean

  7. Re: print float and double in minix

    On Jul 1, 7:01 am, "J.F. de Smit" wrote:
    > puntino wrote:
    > > I wanted my server to do some operations like to calculate average
    > > time and so on..

    >
    > What you could do is manually format the output: convert float f to int
    > i1, deduct i1 from f, multiply f by 100 (for 2 decimal digits) and convert
    > f to another int i2. You can then use
    >
    > printf("%d.%02d", i1, i2);
    >
    > to get your desired output (the 02d makes sure that the decimal digits are
    > zero-padded). Good luck!
    >

    On Jul 1, 7:01 am, "J.F. de Smit" wrote:
    > puntino wrote:
    > > I wanted my server to do some operations like to calculate average
    > > time and so on..

    >
    > What you could do is manually format the output: convert float f to int
    > i1, deduct i1 from f, multiply f by 100 (for 2 decimal digits) and convert
    > f to another int i2. You can then use
    >
    > printf("%d.%02d", i1, i2);
    >
    > to get your desired output (the 02d makes sure that the decimal digits are
    > zero-padded). Good luck!
    >


    Better yet, use fixed point arithmetic. There usually
    is no need for floating point and it adds a lot of
    unecessary overhead. This can be important in
    realtime embedded applications.

    As long as the sum of the samples is small enough to
    hold in a long, all you need to do is add the values
    up, divide by n (number of samples) to get the whole
    part, scale the remainder by 10**d where d is the
    number of decimal places you want, and then divide
    the result by n.

    If the sum could be too large to fit in a long, you
    can prescale the values before adding them into the
    sum, and accumulate an error value to adjust the
    average and compute the fractional part.

    In the example below, I am assuming the samples are
    ints. If they are longs, then the prescaled method
    should be used (you need to change some
    declarations, but I'll leave that as an exercise):

    ------
    #include

    int main( int argc, char **argv ) {
    int sample[] = { 100, 10000, 1234567, 1000000 };
    long sum;
    int i;
    int fract_part, whole_part, error;
    int n = sizeof( sample ) / sizeof( sample[0] );

    /* simple summing method */
    for ( i = 0, sum = 0; i < n; i++ ) {
    sum += sample[i];
    }

    printf( "n = %d, sum = %ld\n", n, sum );
    whole_part = sum / n;
    fract_part = (int)( sum % n ) * 100 / n;
    printf("%ld.%02d\n", whole_part, fract_part);

    /* prescaling method - more divides, slower */
    for ( i = 0, whole_part = 0, error = 0; i < n; i++ ) {
    whole_part += sample[i] / n;
    error += sample[i] % n;
    }
    whole_part += error / n;
    fract_part = ( error % n ) * 100 / n;
    printf("%ld.%02d\n", whole_part, fract_part);
    }

    ------
    (Please note the above code is not tested)

    If the goal is to smooth out a periodic measurement,
    then a filter function might be a better choice.

    The general idea is to keep a running average and
    to adjust it with each new sample:

    A simple filter (can't remember whether this is an
    IIR or FIR):

    average = average * (1 - weight) + sample * weight

    where weight is a fractional value less than 1.

    This can be rewritten to use integer arithmetic,
    but I won't do that here.

    Oh, and if your problem is the function to get
    temperature returns a float, then I would rewrite
    that function to use integer arithmetic and to
    return temperature prescaled to tenths of a
    degree (C or F).

  8. Re: print float and double in minix

    philg wrote:
    > In the example below, I am assuming the samples are
    > ints. If they are longs, then the prescaled method
    > should be used (you need to change some
    > declarations, but I'll leave that as an exercise):


    As far as my knowledge reaches, int and long are equivalent in GCC
    for 32-bit x86 architectures. You can of course use a long long to sum
    large amounts of integers without overflowing.

    Regards,

    Jens

    --
    Jens de Smit
    Student Computer Science | Vrije Universiteit Amsterdam
    jfdsmit@few.vu.nl | http://www.few.vu.nl/~jfdsmit
    "[In the end, people] get furious at IT that the goddamn magic isn't working"
    -- Stewart Dean

  9. Re: print float and double in minix

    On Jul 4, 2:12 am, "J.F. de Smit" wrote:
    >
    > As far as my knowledge reaches, int and long are equivalent in GCC
    > for 32-bit x86 architectures. You can of course use a long long to sum
    > large amounts of integers without overflowing.
    >

    Well, I guess my experience with mostly 8- and 16-bit micros betrays
    itself.

    The algorithm will scale to any word size, but the main idea is to
    avoid unecessary floating-point arithmetic in the kernel and drivers,
    especially if the floating-point is implemented in software.

+ Reply to Thread