I have the following:
typedef union
{
unsigned Color;
struct
{
unsigned char B, G, R, A;
};
} RGB, *PRGB;
Then I have:
RGB Rgb(XYZ Xyz)
{
RGB Result;
double X = Xyz.X / 100;
double Y = Xyz.Y / 100;
double Z = Xyz.Z / 100;
double Red = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
double Green = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
double Blue = X * 0.0557 + Y * -0.2040 + Z * 1.0570;
Red = (Red > 0.0031308) ? (1.055 * pow(Red, (1/2.4)) - 0.055) : 12.92 * Red;
Green = (Green > 0.0031308) ? (1.055 * pow(Green, (1/2.4)) - 0.055) : 12.92 * Green;
Blue = (Blue > 0.0031308) ? (1.055 * pow(Blue, (1/2.4)) - 0.055) : 12.92 * Blue;
//Printing Red here and blue here are perfectly fine..
//The problem is here..
Result.R = (unsigned char)round(Red * 255);
Result.G = (unsigned char)round(Green * 255);
Result.B = (unsigned char)round(Blue * 255);
return Result;
}
In that function above, my values overflow even though I rounded it. When printed, it prints random values which I know is an overflow.
If I change all the doubles to floats, it works perfectly fine and prints the correct value every time. Why? How can I keep it as double? Or should I just change it all to float? I wanted double because it holds more decimal places..