// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using UnityEngine; namespace Doozy.Runtime.Common.Extensions { /// Extension methods for the float (single) struct public static class FloatExtensions { // ReSharper disable once MemberCanBePrivate.Global /// Tolerance value used for float comparisons public const float k_Tolerance = 0.0001f; /// Round number with the given number of decimals /// Target number /// Number of decimals /// Rounded float public static float Round(this float target, int decimals = 1) => (float)Math.Round(target, decimals); /// Clamp the value between the given minimum float and maximum float values /// Target float /// The minimum floating point value to compare against /// The maximum floating point value to compare against /// Clamped float public static float Clamp(this float target, float min, float max) => Mathf.Clamp(target, min, max); /// Clamp value between 0 and 1 /// Target float /// Clamped float public static float Clamp01(this float target) => Mathf.Clamp01(target); /// Compare two floating point values and returns true if they are similar /// Target floating point /// Other floating point to compare against /// TRUE if the values are similar and FALSE otherwise public static bool Approximately(this float target, float otherValue) => Approximately(target, otherValue, k_Tolerance); /// Compare two floating point values and returns true if they are similar /// Target floating point /// Other floating point to compare against /// Tolerance value /// TRUE if the values are similar and FALSE otherwise public static bool Approximately(this float target, float otherValue, float tolerance) => Mathf.Abs(target - otherValue) < tolerance; /// Returns the absolute value of the given float /// Target float /// Absolute value of the given float public static float Abs(this float target) => Mathf.Abs(target); /// Returns the smallest integer greater to or equal to the given float /// Target float /// Smallest integer greater to or equal to the given float public static float RoundToMultiple(this float target, float multiple) { float remainder = target % multiple; if (remainder < 0.5f) return target - remainder; return target + multiple - remainder; } /// Returns the smallest integer greater to or equal to the given float public static float RoundToMultiple(this float target, int multiple) => target.RoundToMultiple((float)multiple); /// Returns the smallest integer greater to or equal to the given float public static float RoundToMultiple(this float target, float multiple, float offset) { float remainder = target % multiple; if (remainder < offset) return target - remainder; return target + multiple - remainder; } /// Returns the smallest integer greater to or equal to the given float /// Target float /// Other value /// Tolerance (step) public static bool CloseTo(this float target, float otherValue, float tolerance) => Mathf.Abs(target - otherValue) <= tolerance; } }