Bug #300
TTLimitMin does not work correctly with unsigned integers
| Status: | Closed | Start date: | 2009-09-05 | |
|---|---|---|---|---|
| Priority: | Immediate | Due date: | ||
| Assignee: | - | % Done: | 100% |
|
| Category: | - | Spent time: | - | |
| Target version: | - | |||
| Branch: | OS: |
Description
Consider this:
TTUInt16 u = 0; TTUInt16 v = TTLimitMin<TTUInt16>(u, 1);
Expected result is 1. Actual result is 32768.
Found while debugging the SpatDisplay extension in TTGraphics.
Mac, Intel, 32-bit
I can see why this happens, but it is not clear what the best way is to address it. For the moment I will modify the SpatDisplay code to use a signed int, but we need to fix this problem.
This would make an ideal regression/unit test candidate (for when we finally write some sort of unit test stuff into the Foundation).
Associated revisions
TTFoundation: incorporating Tristan's template specialization for limitMin to fix bugs operating on unsigned datatypes. Closes #300.
History
Updated by Tristan Matthews over 2 years ago
What about a template specialization for unsigned values?
template <>
TTUInt16 limitMin(TTUInt16 value, TTUInt16 low_bound)
{
value -= std::min(low_bound, value);
#ifdef TT_PLATFORM_MAC
value += fabs(value);
#else
value = TTUInt16(value + fabs((double)value));
#endif
value = TTUInt16(value * 0.5);
value = TTUInt16(value + low_bound);
return value;
}
Updated by Tristan Matthews over 2 years ago
- File limitMin.cpp added
Updated by Anonymous over 2 years ago
- Status changed from New to Closed
- % Done changed from 0 to 100
Applied in changeset cafae77796e2df506c6440039094daed9e82db56.
Updated by Tristan Matthews over 2 years ago
Just wondering why in the specializations you use
template <class T>
TTUInt64 limitMin(TTUInt64 value, TTUInt64 low_bound)
instead of
template <>
TTUInt64 limitMin(TTUInt64 value, TTUInt64 low_bound)
since T isn't used in the functions.
Updated by Tim Place over 2 years ago
Yeah -- Xcode gave me linker errors without the T, complaining about duplicate symbols if the template was used in more than one source file.