0

It may be a dumb thing that I didn't saw but I've got the a really strange behavior on my program.

Here's my code

#include <stdio.h>
void spectral_color(double r,double g,double b,double l); //https://stackoverflow.com/a/22681410

int main(int argc, char **argv)
{
    double r,g,b;
    spectral_color(r,g,b,600);
    //printf("%lf,%lf,%lf",r,g,b);
}

void spectral_color(double r,double g,double b,double l) // RGB <0,1> <- lambda l <400,700> [nm]
    {
    double t;  r=0.0; g=0.0; b=0.0;
    if ((l>=595.0)&&(l<650.0)) { 
        t=(l-595.0)/(650.0-595.0); 
        r=(double)0.98+(0.06*t)-(0.40*t*t); 
    }
    printf("%lf,%lf,%lf",r,g,b);
}

It prints 0.000000,0.000000,0.000000 and I tried this in the gdb

15          t=(l-595.0)/(650.0-595.0); 
(gdb) p l
$8 = 600
(gdb) p r
$9 = 1.7802772279180279e-307
(gdb) p t
$10 = 2.4846118548532686e+264
(gdb) n
16          r=(double)0.98+(0.06*t)-(0.40*t*t); 
(gdb) p t
$11 = 0.090909090909090912
(gdb) p r
$12 = 1.7802772279180279e-307
(gdb) n
18      printf("%lf,%lf,%lf",r,g,b);
(gdb) p t
$13 = 0.090909090909090912
(gdb) p r
$14 = 1.7802772279180279e-307

Here, r do not take the value it should and I have no idea why it would do that. Do someone have an explanation ?

Thanks in advance

edit:

I passed the value of r to another variable and changed the %lf to %f, the result is that r is never equal to the new variable but the correct value ends up in this new variable

double d = r; //this is line 18
printf("%f,%f,%f",d,g,b);

gdb result:

20  }
(gdb) p d
$1 = 0.98214876033057852
(gdb) p r
$2 = 1.7802772279180279e-307
(gdb)
Tix'at
  • 3
  • 4

1 Answers1

-1

Once I have been bothered by this problem until I read the manual

This is the explanation of the l length modifier

(ell) A following integer conversion corresponds to a long int or unsigned long int argument, or a following n conversion corresponds to a pointer to a long int argument, or a following c conversion corresponds to a wint_t argument, or a following s conversion corresponds to a pointer to wchar_t argument.

Maybe a picture could be more illustrating enter image description here

There would be a type promotion or conversion performed if you use the length modifier of l for a double. That's probably the reason of your confusion.

Paul
  • 1,630
  • 1
  • 16
  • 23
  • *"By the way, I always use `L` for a `float` or `double` conversion."* That makes no sense at all. As shown in your table, uppercase `L` is reserved for `long double`. – user694733 Jun 15 '17 at 09:18
  • *"There would be a type promotion or conversion performed if you use the length modifier of `l` for a `double`"* Length modifiers don't affect type promotion. `double` is always passed as `double` to `printf` unless there is some non-standard compiler behaviour going on. – user694733 Jun 15 '17 at 09:25
  • I don't see how you using `long double` is relevant to the problem. Question doesn't use `long double`. – user694733 Jun 15 '17 at 09:28
  • @user694733 Maybe I was totally wrong. Could you please tell me why the `l` length modifier causes the `double` to `0`? – Paul Jun 15 '17 at 09:45