Bug #300

TTLimitMin does not work correctly with unsigned integers

Added by Tim Place over 2 years ago. Updated over 2 years ago.

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).

limitMin.cpp - sample implementation and test (2.7 kB) Tristan Matthews, 09/06/2009 10:19 pm

Associated revisions

Revision cafae777
Added by Timothy Place over 2 years ago

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

Updated by Anonymous over 2 years ago

  • Status changed from New to Closed
  • % Done changed from 0 to 100

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.

Also available in: Atom PDF