OpenShot Library | OpenShotAudio  0.2.1
juce_MathsFunctions.h
1 
2 /** @weakgroup juce_core-maths
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /*
32  This file sets up some handy mathematical typdefs and functions.
33 */
34 
35 //==============================================================================
36 // Definitions for the int8, int16, int32, int64 and pointer_sized_int types.
37 
38 /** A platform-independent 8-bit signed integer type. */
39 using int8 = signed char;
40 /** A platform-independent 8-bit unsigned integer type. */
41 using uint8 = unsigned char;
42 /** A platform-independent 16-bit signed integer type. */
43 using int16 = signed short;
44 /** A platform-independent 16-bit unsigned integer type. */
45 using uint16 = unsigned short;
46 /** A platform-independent 32-bit signed integer type. */
47 using int32 = signed int;
48 /** A platform-independent 32-bit unsigned integer type. */
49 using uint32 = unsigned int;
50 
51 #if JUCE_MSVC
52  /** A platform-independent 64-bit integer type. */
53  using int64 = __int64;
54  /** A platform-independent 64-bit unsigned integer type. */
55  using uint64 = unsigned __int64;
56 #else
57  /** A platform-independent 64-bit integer type. */
58  using int64 = long long;
59  /** A platform-independent 64-bit unsigned integer type. */
60  using uint64 = unsigned long long;
61 #endif
62 
63 #ifndef DOXYGEN
64  /** A macro for creating 64-bit literals.
65  Historically, this was needed to support portability with MSVC6, and is kept here
66  so that old code will still compile, but nowadays every compiler will support the
67  LL and ULL suffixes, so you should use those in preference to this macro.
68  */
69  #define literal64bit(longLiteral) (longLiteral##LL)
70 #endif
71 
72 #if JUCE_64BIT
73  /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
74  using pointer_sized_int = int64;
75  /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
76  using pointer_sized_uint = uint64;
77 #elif JUCE_MSVC
78  /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
79  using pointer_sized_int = _W64 int;
80  /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
81  using pointer_sized_uint = _W64 unsigned int;
82 #else
83  /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
84  using pointer_sized_int = int;
85  /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
86  using pointer_sized_uint = unsigned int;
87 #endif
88 
89 #if JUCE_WINDOWS && ! JUCE_MINGW
90  using ssize_t = pointer_sized_int;
91 #endif
92 
93 //==============================================================================
94 // Some indispensable min/max functions
95 
96 /** Returns the larger of two values. */
97 template <typename Type>
98 JUCE_CONSTEXPR Type jmax (Type a, Type b) { return a < b ? b : a; }
99 
100 /** Returns the larger of three values. */
101 template <typename Type>
102 JUCE_CONSTEXPR Type jmax (Type a, Type b, Type c) { return a < b ? (b < c ? c : b) : (a < c ? c : a); }
103 
104 /** Returns the larger of four values. */
105 template <typename Type>
106 JUCE_CONSTEXPR Type jmax (Type a, Type b, Type c, Type d) { return jmax (a, jmax (b, c, d)); }
107 
108 /** Returns the smaller of two values. */
109 template <typename Type>
110 JUCE_CONSTEXPR Type jmin (Type a, Type b) { return b < a ? b : a; }
111 
112 /** Returns the smaller of three values. */
113 template <typename Type>
114 JUCE_CONSTEXPR Type jmin (Type a, Type b, Type c) { return b < a ? (c < b ? c : b) : (c < a ? c : a); }
115 
116 /** Returns the smaller of four values. */
117 template <typename Type>
118 JUCE_CONSTEXPR Type jmin (Type a, Type b, Type c, Type d) { return jmin (a, jmin (b, c, d)); }
119 
120 /** Remaps a normalised value (between 0 and 1) to a target range.
121  This effectively returns (targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin)).
122 */
123 template <typename Type>
124 JUCE_CONSTEXPR Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
125 {
126  return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
127 }
128 
129 /** Remaps a value from a source range to a target range. */
130 template <typename Type>
131 Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
132 {
133  jassert (sourceRangeMax != sourceRangeMin); // mapping from a range of zero will produce NaN!
134  return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
135 }
136 
137 /** Scans an array of values, returning the minimum value that it contains. */
138 template <typename Type>
139 Type findMinimum (const Type* data, int numValues)
140 {
141  if (numValues <= 0)
142  return Type (0);
143 
144  auto result = *data++;
145 
146  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
147  {
148  auto v = *data++;
149 
150  if (v < result)
151  result = v;
152  }
153 
154  return result;
155 }
156 
157 /** Scans an array of values, returning the maximum value that it contains. */
158 template <typename Type>
159 Type findMaximum (const Type* values, int numValues)
160 {
161  if (numValues <= 0)
162  return Type (0);
163 
164  auto result = *values++;
165 
166  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
167  {
168  auto v = *values++;
169 
170  if (result < v)
171  result = v;
172  }
173 
174  return result;
175 }
176 
177 /** Scans an array of values, returning the minimum and maximum values that it contains. */
178 template <typename Type>
179 void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
180 {
181  if (numValues <= 0)
182  {
183  lowest = Type (0);
184  highest = Type (0);
185  }
186  else
187  {
188  auto mn = *values++;
189  auto mx = mn;
190 
191  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
192  {
193  auto v = *values++;
194 
195  if (mx < v) mx = v;
196  if (v < mn) mn = v;
197  }
198 
199  lowest = mn;
200  highest = mx;
201  }
202 }
203 
204 //==============================================================================
205 /** Constrains a value to keep it within a given range.
206 
207  This will check that the specified value lies between the lower and upper bounds
208  specified, and if not, will return the nearest value that would be in-range. Effectively,
209  it's like calling jmax (lowerLimit, jmin (upperLimit, value)).
210 
211  Note that it expects that lowerLimit <= upperLimit. If this isn't true,
212  the results will be unpredictable.
213 
214  @param lowerLimit the minimum value to return
215  @param upperLimit the maximum value to return
216  @param valueToConstrain the value to try to return
217  @returns the closest value to valueToConstrain which lies between lowerLimit
218  and upperLimit (inclusive)
219  @see jmin, jmax, jmap
220 */
221 template <typename Type>
222 Type jlimit (Type lowerLimit,
223  Type upperLimit,
224  Type valueToConstrain) noexcept
225 {
226  jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
227 
228  return valueToConstrain < lowerLimit ? lowerLimit
229  : (upperLimit < valueToConstrain ? upperLimit
230  : valueToConstrain);
231 }
232 
233 /** Returns true if a value is at least zero, and also below a specified upper limit.
234  This is basically a quicker way to write:
235  @code valueToTest >= 0 && valueToTest < upperLimit
236  @endcode
237 */
238 template <typename Type1, typename Type2>
239 bool isPositiveAndBelow (Type1 valueToTest, Type2 upperLimit) noexcept
240 {
241  jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
242  return Type1() <= valueToTest && valueToTest < static_cast<Type1> (upperLimit);
243 }
244 
245 template <typename Type>
246 bool isPositiveAndBelow (int valueToTest, Type upperLimit) noexcept
247 {
248  jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
249  return static_cast<unsigned int> (valueToTest) < static_cast<unsigned int> (upperLimit);
250 }
251 
252 /** Returns true if a value is at least zero, and also less than or equal to a specified upper limit.
253  This is basically a quicker way to write:
254  @code valueToTest >= 0 && valueToTest <= upperLimit
255  @endcode
256 */
257 template <typename Type1, typename Type2>
258 bool isPositiveAndNotGreaterThan (Type1 valueToTest, Type2 upperLimit) noexcept
259 {
260  jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
261  return Type1() <= valueToTest && valueToTest <= static_cast<Type1> (upperLimit);
262 }
263 
264 template <typename Type>
265 bool isPositiveAndNotGreaterThan (int valueToTest, Type upperLimit) noexcept
266 {
267  jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
268  return static_cast<unsigned int> (valueToTest) <= static_cast<unsigned int> (upperLimit);
269 }
270 
271 /** Computes the absolute difference between two values and returns true if it is less than or equal
272  to a given tolerance, otherwise it returns false.
273 */
274 template <typename Type>
275 bool isWithin (Type a, Type b, Type tolerance) noexcept
276 {
277  return std::abs (a - b) <= tolerance;
278 }
279 
280 /** Returns true if the two numbers are approximately equal. This is useful for floating-point
281  and double comparisons.
282 */
283 template <typename Type>
284 bool approximatelyEqual (Type a, Type b) noexcept
285 {
286  return std::abs (a - b) <= (std::numeric_limits<Type>::epsilon() * std::max (a, b))
287  || std::abs (a - b) < std::numeric_limits<Type>::min();
288 }
289 
290 //==============================================================================
291 /** Handy function for avoiding unused variables warning. */
292 template <typename... Types>
293 void ignoreUnused (Types&&...) noexcept {}
294 
295 /** Handy function for getting the number of elements in a simple const C array.
296  E.g.
297  @code
298  static int myArray[] = { 1, 2, 3 };
299 
300  int numElements = numElementsInArray (myArray) // returns 3
301  @endcode
302 */
303 template <typename Type, size_t N>
304 JUCE_CONSTEXPR int numElementsInArray (Type (&)[N]) noexcept { return N; }
305 
306 //==============================================================================
307 // Some useful maths functions that aren't always present with all compilers and build settings.
308 
309 /** Using juce_hypot is easier than dealing with the different types of hypot function
310  that are provided by the various platforms and compilers. */
311 template <typename Type>
312 Type juce_hypot (Type a, Type b) noexcept
313 {
314  #if JUCE_MSVC
315  return static_cast<Type> (_hypot (a, b));
316  #else
317  return static_cast<Type> (hypot (a, b));
318  #endif
319 }
320 
321 #ifndef DOXYGEN
322 template <>
323 inline float juce_hypot (float a, float b) noexcept
324 {
325  #if JUCE_MSVC
326  return _hypotf (a, b);
327  #else
328  return hypotf (a, b);
329  #endif
330 }
331 #endif
332 
333 //==============================================================================
334 #if JUCE_HAS_CONSTEXPR
335 
336 /** Commonly used mathematical constants
337 
338  @tags{Core}
339 */
340 template <typename FloatType>
341 struct MathConstants
342 {
343  /** A predefined value for Pi */
344  static constexpr FloatType pi = static_cast<FloatType> (3.141592653589793238L);
345 
346  /** A predefined value for 2 * Pi */
347  static constexpr FloatType twoPi = static_cast<FloatType> (2 * 3.141592653589793238L);
348 
349  /** A predefined value for Pi / 2 */
350  static constexpr FloatType halfPi = static_cast<FloatType> (3.141592653589793238L / 2);
351 
352  /** A predefined value for Euler's number */
353  static constexpr FloatType euler = static_cast<FloatType> (2.71828182845904523536L);
354 
355  /** A predefined value for sqrt(2) */
356  static constexpr FloatType sqrt2 = static_cast<FloatType> (1.4142135623730950488L);
357 };
358 
359 #else
360 
361 /** Commonly used mathematical constants
362 
363  @tags{Core}
364 */
365 template <typename FloatType>
367 {
368  /** A predefined value for Pi */
369  static const FloatType pi;
370 
371  /** A predefined value for 2 * Pi */
372  static const FloatType twoPi;
373 
374  /** A predefined value for Pi / 2 */
375  static const FloatType halfPi;
376 
377  /** A predefined value for Euler's number */
378  static const FloatType euler;
379 
380  /** A predefined value for sqrt(2) */
381  static const FloatType sqrt2;
382 };
383 
384 template <typename FloatType>
385 const FloatType MathConstants<FloatType>::pi = static_cast<FloatType> (3.141592653589793238L);
386 
387 template <typename FloatType>
388 const FloatType MathConstants<FloatType>::twoPi = static_cast<FloatType> (2 * 3.141592653589793238L);
389 
390 template <typename FloatType>
391 const FloatType MathConstants<FloatType>::halfPi = static_cast<FloatType> (3.141592653589793238L / 2);
392 
393 template <typename FloatType>
394 const FloatType MathConstants<FloatType>::euler = static_cast<FloatType> (2.71828182845904523536L);
395 
396 template <typename FloatType>
397 const FloatType MathConstants<FloatType>::sqrt2 = static_cast<FloatType> (1.4142135623730950488L);
398 
399 #endif
400 
401 #ifndef DOXYGEN
402 /** A double-precision constant for pi.
403  @deprecated This is deprecated in favour of MathConstants<double>::pi.
404  The reason is that "double_Pi" was a confusing name, and many people misused it,
405  wrongly thinking it meant 2 * pi !
406 */
407 const JUCE_CONSTEXPR double double_Pi = MathConstants<double>::pi;
408 
409 /** A single-precision constant for pi.
410  @deprecated This is deprecated in favour of MathConstants<float>::pi.
411  The reason is that "double_Pi" was a confusing name, and many people misused it,
412  wrongly thinking it meant 2 * pi !
413 */
414 const JUCE_CONSTEXPR float float_Pi = MathConstants<float>::pi;
415 #endif
416 
417 /** Converts an angle in degrees to radians. */
418 template <typename FloatType>
419 JUCE_CONSTEXPR FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * (MathConstants<FloatType>::pi / FloatType (180)); }
420 
421 /** Converts an angle in radians to degrees. */
422 template <typename FloatType>
423 JUCE_CONSTEXPR FloatType radiansToDegrees (FloatType radians) noexcept { return radians * (FloatType (180) / MathConstants<FloatType>::pi); }
424 
425 
426 //==============================================================================
427 /** The isfinite() method seems to vary between platforms, so this is a
428  platform-independent function for it.
429 */
430 template <typename NumericType>
431 bool juce_isfinite (NumericType) noexcept
432 {
433  return true; // Integer types are always finite
434 }
435 
436 template <>
437 inline bool juce_isfinite (float value) noexcept
438 {
439  #if JUCE_WINDOWS && ! JUCE_MINGW
440  return _finite (value) != 0;
441  #else
442  return std::isfinite (value);
443  #endif
444 }
445 
446 template <>
447 inline bool juce_isfinite (double value) noexcept
448 {
449  #if JUCE_WINDOWS && ! JUCE_MINGW
450  return _finite (value) != 0;
451  #else
452  return std::isfinite (value);
453  #endif
454 }
455 
456 //==============================================================================
457 #if JUCE_MSVC
458  #pragma optimize ("t", off)
459  #ifndef __INTEL_COMPILER
460  #pragma float_control (precise, on, push)
461  #endif
462 #endif
463 
464 /** Fast floating-point-to-integer conversion.
465 
466  This is faster than using the normal c++ cast to convert a float to an int, and
467  it will round the value to the nearest integer, rather than rounding it down
468  like the normal cast does.
469 
470  Note that this routine gets its speed at the expense of some accuracy, and when
471  rounding values whose floating point component is exactly 0.5, odd numbers and
472  even numbers will be rounded up or down differently.
473 */
474 template <typename FloatType>
475 int roundToInt (const FloatType value) noexcept
476 {
477  #ifdef __INTEL_COMPILER
478  #pragma float_control (precise, on, push)
479  #endif
480 
481  union { int asInt[2]; double asDouble; } n;
482  n.asDouble = ((double) value) + 6755399441055744.0;
483 
484  #if JUCE_BIG_ENDIAN
485  return n.asInt [1];
486  #else
487  return n.asInt [0];
488  #endif
489 }
490 
491 inline int roundToInt (int value) noexcept
492 {
493  return value;
494 }
495 
496 #if JUCE_MSVC
497  #ifndef __INTEL_COMPILER
498  #pragma float_control (pop)
499  #endif
500  #pragma optimize ("", on) // resets optimisations to the project defaults
501 #endif
502 
503 /** Fast floating-point-to-integer conversion.
504 
505  This is a slightly slower and slightly more accurate version of roundToInt(). It works
506  fine for values above zero, but negative numbers are rounded the wrong way.
507 */
508 inline int roundToIntAccurate (double value) noexcept
509 {
510  #ifdef __INTEL_COMPILER
511  #pragma float_control (pop)
512  #endif
513 
514  return roundToInt (value + 1.5e-8);
515 }
516 
517 //==============================================================================
518 /** Truncates a positive floating-point number to an unsigned int.
519 
520  This is generally faster than static_cast<unsigned int> (std::floor (x))
521  but it only works for positive numbers small enough to be represented as an
522  unsigned int.
523 */
524 template <typename FloatType>
525 unsigned int truncatePositiveToUnsignedInt (FloatType value) noexcept
526 {
527  jassert (value >= static_cast<FloatType> (0));
528  jassert (static_cast<FloatType> (value) <= std::numeric_limits<unsigned int>::max());
529 
530  return static_cast<unsigned int> (value);
531 }
532 
533 //==============================================================================
534 /** Returns true if the specified integer is a power-of-two. */
535 template <typename IntegerType>
536 JUCE_CONSTEXPR bool isPowerOfTwo (IntegerType value)
537 {
538  return (value & (value - 1)) == 0;
539 }
540 
541 /** Returns the smallest power-of-two which is equal to or greater than the given integer. */
542 inline int nextPowerOfTwo (int n) noexcept
543 {
544  --n;
545  n |= (n >> 1);
546  n |= (n >> 2);
547  n |= (n >> 4);
548  n |= (n >> 8);
549  n |= (n >> 16);
550  return n + 1;
551 }
552 
553 /** Returns the index of the highest set bit in a (non-zero) number.
554  So for n=3 this would return 1, for n=7 it returns 2, etc.
555  An input value of 0 is illegal!
556 */
557 int findHighestSetBit (uint32 n) noexcept;
558 
559 /** Returns the number of bits in a 32-bit integer. */
560 inline int countNumberOfBits (uint32 n) noexcept
561 {
562  n -= ((n >> 1) & 0x55555555);
563  n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
564  n = (((n >> 4) + n) & 0x0f0f0f0f);
565  n += (n >> 8);
566  n += (n >> 16);
567  return (int) (n & 0x3f);
568 }
569 
570 /** Returns the number of bits in a 64-bit integer. */
571 inline int countNumberOfBits (uint64 n) noexcept
572 {
573  return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
574 }
575 
576 /** Performs a modulo operation, but can cope with the dividend being negative.
577  The divisor must be greater than zero.
578 */
579 template <typename IntegerType>
580 IntegerType negativeAwareModulo (IntegerType dividend, const IntegerType divisor) noexcept
581 {
582  jassert (divisor > 0);
583  dividend %= divisor;
584  return (dividend < 0) ? (dividend + divisor) : dividend;
585 }
586 
587 /** Returns the square of its argument. */
588 template <typename NumericType>
589 inline JUCE_CONSTEXPR NumericType square (NumericType n) noexcept
590 {
591  return n * n;
592 }
593 
594 //==============================================================================
595 /** Writes a number of bits into a memory buffer at a given bit index.
596  The buffer is treated as a sequence of 8-bit bytes, and the value is encoded in little-endian order,
597  so for example if startBit = 10, and numBits = 11 then the lower 6 bits of the value would be written
598  into bits 2-8 of targetBuffer[1], and the upper 5 bits of value into bits 0-5 of targetBuffer[2].
599 
600  @see readLittleEndianBitsInBuffer
601 */
602 void writeLittleEndianBitsInBuffer (void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept;
603 
604 /** Reads a number of bits from a buffer at a given bit index.
605  The buffer is treated as a sequence of 8-bit bytes, and the value is encoded in little-endian order,
606  so for example if startBit = 10, and numBits = 11 then the lower 6 bits of the result would be read
607  from bits 2-8 of sourceBuffer[1], and the upper 5 bits of the result from bits 0-5 of sourceBuffer[2].
608 
609  @see writeLittleEndianBitsInBuffer
610 */
611 uint32 readLittleEndianBitsInBuffer (const void* sourceBuffer, uint32 startBit, uint32 numBits) noexcept;
612 
613 
614 //==============================================================================
615 #if JUCE_INTEL || defined (DOXYGEN)
616  /** This macro can be applied to a float variable to check whether it contains a denormalised
617  value, and to normalise it if necessary.
618  On CPUs that aren't vulnerable to denormalisation problems, this will have no effect.
619  */
620  #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
621 #else
622  #define JUCE_UNDENORMALISE(x)
623 #endif
624 
625 //==============================================================================
626 /** This namespace contains a few template classes for helping work out class type variations.
627 */
628 namespace TypeHelpers
629 {
630  /** The ParameterType struct is used to find the best type to use when passing some kind
631  of object as a parameter.
632 
633  Of course, this is only likely to be useful in certain esoteric template situations.
634 
635  E.g. "myFunction (typename TypeHelpers::ParameterType<int>::type, typename TypeHelpers::ParameterType<MyObject>::type)"
636  would evaluate to "myfunction (int, const MyObject&)", keeping any primitive types as
637  pass-by-value, but passing objects as a const reference, to avoid copying.
638 
639  @tags{Core}
640  */
641  template <typename Type> struct ParameterType { using type = const Type&; };
642 
643  #if ! DOXYGEN
644  template <typename Type> struct ParameterType <Type&> { using type = Type&; };
645  template <typename Type> struct ParameterType <Type*> { using type = Type*; };
646  template <> struct ParameterType <char> { using type = char; };
647  template <> struct ParameterType <unsigned char> { using type = unsigned char; };
648  template <> struct ParameterType <short> { using type = short; };
649  template <> struct ParameterType <unsigned short> { using type = unsigned short; };
650  template <> struct ParameterType <int> { using type = int; };
651  template <> struct ParameterType <unsigned int> { using type = unsigned int; };
652  template <> struct ParameterType <long> { using type = long; };
653  template <> struct ParameterType <unsigned long> { using type = unsigned long; };
654  template <> struct ParameterType <int64> { using type = int64; };
655  template <> struct ParameterType <uint64> { using type = uint64; };
656  template <> struct ParameterType <bool> { using type = bool; };
657  template <> struct ParameterType <float> { using type = float; };
658  template <> struct ParameterType <double> { using type = double; };
659  #endif
660 
661  /** These templates are designed to take a type, and if it's a double, they return a double
662  type; for anything else, they return a float type.
663 
664  @tags{Core}
665  */
666  template <typename Type> struct SmallestFloatType { using type = float; };
667 
668  #if ! DOXYGEN
669  template <> struct SmallestFloatType <double> { using type = double; };
670  #endif
671 
672  /** These templates are designed to take an integer type, and return an unsigned int
673  version with the same size.
674 
675  @tags{Core}
676  */
677  template <int bytes> struct UnsignedTypeWithSize {};
678 
679  #if ! DOXYGEN
680  template <> struct UnsignedTypeWithSize<1> { using type = uint8; };
681  template <> struct UnsignedTypeWithSize<2> { using type = uint16; };
682  template <> struct UnsignedTypeWithSize<4> { using type = uint32; };
683  template <> struct UnsignedTypeWithSize<8> { using type = uint64; };
684  #endif
685 }
686 
687 //==============================================================================
688 #if ! DOXYGEN
689  // These old functions are deprecated: Just use roundToInt instead.
690  JUCE_DEPRECATED_ATTRIBUTE inline int roundDoubleToInt (double value) noexcept { return roundToInt (value); }
691  JUCE_DEPRECATED_ATTRIBUTE inline int roundFloatToInt (float value) noexcept { return roundToInt (value); }
692 
693  // This old function isn't needed - just use std::abs() instead
694  JUCE_DEPRECATED_ATTRIBUTE inline int64 abs64 (int64 n) noexcept { return std::abs (n); }
695 #endif
696 
697 } // namespace juce
698 
699 /** @}*/
These templates are designed to take an integer type, and return an unsigned int version with the sam...
These templates are designed to take a type, and if it&#39;s a double, they return a double type; for any...
static const FloatType pi
A predefined value for Pi.
The ParameterType struct is used to find the best type to use when passing some kind of object as a p...
static const FloatType halfPi
A predefined value for Pi / 2.
static const FloatType euler
A predefined value for Euler&#39;s number.
static const FloatType sqrt2
A predefined value for sqrt(2)
static const FloatType twoPi
A predefined value for 2 * Pi.
Commonly used mathematical constants.