m3m0ry / fixedpoint

Decimal fixed point type
Boost Software License 1.0
6 stars 3 forks source link

Fixed does not use value type for ctor. #7

Closed schveiguy closed 3 years ago

schveiguy commented 3 years ago

The ctor accepts any type that is an integral. But then uses the type of the integral to do the math. The entire constructor is:

    this(T)(const T i) if (isIntegral!T)
    {
        value = i * factor;
    }

Consider the value int.max as a parameter to a Fixed!2 struct. This results in assigning the value int.max * 100, which ends up being -100. However, a Fixed!2 based on long is perfectly capable of storing the correct value of 214_748_364_700.

Not only that, but even if the incoming type is larger than the stored type, it's not going to compile. I think the ctor should simply accept a V instead of a templated type. That should fix this bug.