diff --git a/examples/DSPDemo/Source/Demos/StateVariableFilterDemo.cpp b/examples/DSPDemo/Source/Demos/StateVariableFilterDemo.cpp index 77f8757379..2992d0e186 100644 --- a/examples/DSPDemo/Source/Demos/StateVariableFilterDemo.cpp +++ b/examples/DSPDemo/Source/Demos/StateVariableFilterDemo.cpp @@ -67,7 +67,7 @@ struct StateVariableFilterDemo ChoiceParameter typeParam {{ "Low-pass", "Band-pass", "High-pass"}, 1, "Type" }; SliderParameter cutoffParam {{ 20.0, 20000.0 }, 0.5, 440.0f, "Cutoff", "Hz" }; - SliderParameter qParam {{ 0.3, 20.0 }, 0.5, 1.0 / std::sqrt (2.0), "Resonance" }; + SliderParameter qParam {{ 0.3, 20.0 }, 0.5, 1.0 / MathConstants::sqrt2, "Resonance" }; std::vector parameters { &typeParam, &cutoffParam, &qParam }; double sampleRate = 0; diff --git a/modules/juce_audio_basics/effects/juce_IIRFilter.cpp b/modules/juce_audio_basics/effects/juce_IIRFilter.cpp index 50fb28da6c..392ccfc1b6 100644 --- a/modules/juce_audio_basics/effects/juce_IIRFilter.cpp +++ b/modules/juce_audio_basics/effects/juce_IIRFilter.cpp @@ -56,7 +56,7 @@ IIRCoefficients::IIRCoefficients (double c1, double c2, double c3, IIRCoefficients IIRCoefficients::makeLowPass (double sampleRate, double frequency) noexcept { - return makeLowPass (sampleRate, frequency, 1.0 / std::sqrt (2.0)); + return makeLowPass (sampleRate, frequency, 1.0 / MathConstants::sqrt2); } IIRCoefficients IIRCoefficients::makeLowPass (double sampleRate, @@ -108,7 +108,7 @@ IIRCoefficients IIRCoefficients::makeHighPass (double sampleRate, IIRCoefficients IIRCoefficients::makeBandPass (double sampleRate, double frequency) noexcept { - return makeBandPass (sampleRate, frequency, 1.0 / std::sqrt (2.0)); + return makeBandPass (sampleRate, frequency, 1.0 / MathConstants::sqrt2); } IIRCoefficients IIRCoefficients::makeBandPass (double sampleRate, @@ -134,7 +134,7 @@ IIRCoefficients IIRCoefficients::makeBandPass (double sampleRate, IIRCoefficients IIRCoefficients::makeNotchFilter (double sampleRate, double frequency) noexcept { - return makeNotchFilter (sampleRate, frequency, 1.0 / std::sqrt (2.0)); + return makeNotchFilter (sampleRate, frequency, 1.0 / MathConstants::sqrt2); } IIRCoefficients IIRCoefficients::makeNotchFilter (double sampleRate, @@ -160,7 +160,7 @@ IIRCoefficients IIRCoefficients::makeNotchFilter (double sampleRate, IIRCoefficients IIRCoefficients::makeAllPass (double sampleRate, double frequency) noexcept { - return makeAllPass (sampleRate, frequency, 1.0 / std::sqrt (2.0)); + return makeAllPass (sampleRate, frequency, 1.0 / MathConstants::sqrt2); } IIRCoefficients IIRCoefficients::makeAllPass (double sampleRate, diff --git a/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp b/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp index 1dcbf51e7b..1377071359 100644 --- a/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp +++ b/modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp @@ -203,14 +203,14 @@ void ResamplingAudioSource::createLowPass (const double frequencyRatio) const double n = 1.0 / std::tan (MathConstants::pi * jmax (0.001, proportionalRate)); const double nSquared = n * n; - const double c1 = 1.0 / (1.0 + std::sqrt (2.0) * n + nSquared); + const double c1 = 1.0 / (1.0 + MathConstants::sqrt2 * n + nSquared); setFilterCoefficients (c1, c1 * 2.0f, c1, 1.0, c1 * 2.0 * (1.0 - nSquared), - c1 * (1.0 - std::sqrt (2.0) * n + nSquared)); + c1 * (1.0 - MathConstants::sqrt2 * n + nSquared)); } void ResamplingAudioSource::setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index f5c06e42f5..9c3b8262de 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -887,8 +887,8 @@ struct JavascriptEngine::RootObject : public DynamicObject private: String::CharPointerType p; - static bool isIdentifierStart (const juce_wchar c) noexcept { return CharacterFunctions::isLetter (c) || c == '_'; } - static bool isIdentifierBody (const juce_wchar c) noexcept { return CharacterFunctions::isLetterOrDigit (c) || c == '_'; } + static bool isIdentifierStart (juce_wchar c) noexcept { return CharacterFunctions::isLetter (c) || c == '_'; } + static bool isIdentifierBody (juce_wchar c) noexcept { return CharacterFunctions::isLetterOrDigit (c) || c == '_'; } TokenType matchNextToken() { @@ -925,7 +925,7 @@ struct JavascriptEngine::RootObject : public DynamicObject return TokenTypes::eof; } - bool matchToken (TokenType name, const size_t len) noexcept + bool matchToken (TokenType name, size_t len) noexcept { if (p.compareUpTo (CharPointer_ASCII (name), (int) len) != 0) return false; p += (int) len; return true; @@ -1701,8 +1701,14 @@ struct JavascriptEngine::RootObject : public DynamicObject setMethod ("sqr", Math_sqr); setMethod ("sqrt", Math_sqrt); setMethod ("ceil", Math_ceil); setMethod ("floor", Math_floor); - setProperty ("PI", MathConstants::pi); - setProperty ("E", exp (1.0)); + setProperty ("PI", MathConstants::pi); + setProperty ("E", MathConstants::euler); + setProperty ("SQRT2", MathConstants::sqrt2); + setProperty ("SQRT1_2", std::sqrt (0.5)); + setProperty ("LN2", std::log (2.0)); + setProperty ("LN10", std::log (10.0)); + setProperty ("LOG2E", std::log (MathConstants::euler) / std::log (2.0)); + setProperty ("LOG10E", std::log (MathConstants::euler) / std::log (10.0)); } static var Math_random (Args) { return Random::getSystemRandom().nextDouble(); } diff --git a/modules/juce_core/maths/juce_MathsFunctions.h b/modules/juce_core/maths/juce_MathsFunctions.h index bb550d9a19..a4dd50956b 100644 --- a/modules/juce_core/maths/juce_MathsFunctions.h +++ b/modules/juce_core/maths/juce_MathsFunctions.h @@ -343,6 +343,9 @@ struct MathConstants /** A predfined value for Euler's number */ static constexpr FloatType euler = static_cast (2.71828182845904523536L); + + /** A predfined value for sqrt(2) */ + static constexpr FloatType sqrt2 = static_cast (1.4142135623730950488L); }; #else @@ -362,6 +365,9 @@ struct MathConstants /** A predfined value for Euler's number */ static const FloatType euler; + + /** A predfined value for sqrt(2) */ + static constexpr FloatType sqrt2; }; template @@ -376,6 +382,9 @@ const FloatType MathConstants::halfPi = static_cast (3.141 template const FloatType MathConstants::euler = static_cast (2.71828182845904523536L); +template +const FloatType MathConstants::sqrt2 = static_cast (1.4142135623730950488L); + #endif diff --git a/modules/juce_dsp/processors/juce_StateVariableFilter.h b/modules/juce_dsp/processors/juce_StateVariableFilter.h index a719ac58dc..de396c8505 100644 --- a/modules/juce_dsp/processors/juce_StateVariableFilter.h +++ b/modules/juce_dsp/processors/juce_StateVariableFilter.h @@ -186,7 +186,7 @@ namespace StateVariableFilter at 1 / sqrt(2). */ void setCutOffFrequency (double sampleRate, NumericType frequency, - NumericType resonance = static_cast (1.0 / std::sqrt (2.0))) noexcept + NumericType resonance = static_cast (1.0 / MathConstants::sqrt2)) noexcept { jassert (sampleRate > 0); jassert (resonance > NumericType (0)); @@ -206,11 +206,11 @@ namespace StateVariableFilter //============================================================================== Parameters() = default; Parameters (const Parameters& o) : g (o.g), R2 (o.R2), h (o.h) {} - Parameters& operator= (const Parameters& o) noexcept { g = o.g; R2 = o.R2; h = o.h; return *this; } + Parameters& operator= (const Parameters& o) noexcept { g = o.g; R2 = o.R2; h = o.h; return *this; } //============================================================================== NumericType g = static_cast (std::tan (MathConstants::pi * 200.0 / 44100.0)); - NumericType R2 = static_cast (std::sqrt (2.0)); + NumericType R2 = static_cast (MathConstants::sqrt2); NumericType h = static_cast (1.0 / (1.0 + R2 * g + g * g)); }; }