There is no 1-to-1 conversion between YCbCr and RGB. Using the same range of values, there are colors in both color spaces that cannot be represented in the other.
When you do a conversion in either direction, you need to clamp the values to the range.
For example, an RGB to Y conversion needs to have something like this for every component conversion:
double RGBtoY (double R, double G, double B)
{
. . . .
if (result < 0.0)
result = 0.0 ;
else if (result > 255) // Or whatever maximum value
result = 255 ;
}
Otherwise, you are going to get wrap around.