File size: 1,467 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// ------------------------------------------------------
/// SwarmOps - Numeric and heuristic optimization for C#
/// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// SwarmOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

namespace SwarmOps
{
    public static partial class Tools
    {
        /// <summary>
        /// The limit below which the number x is considered denormalized.
        /// </summary>
        static readonly double DenormalizeLimit = 1e-30;

        /// <summary>
        /// If Abs(x) is below a limit then it is set to zero.
        /// </summary>
        public static double DenormalizeTruncate(double x)
        {
            return (System.Math.Abs(x) > DenormalizeLimit) ? (x) : (0);
        }

        /// <summary>
        /// Return denormalized version of x. This is useful for
        /// avoiding expensive CPU usage when x underflows.
        /// </summary>
        public static double Denormalize(double x)
        {
            return DenormalizeTruncate(x);
        }

        /// <summary>
        /// Denormalize an array.
        /// </summary>
        public static void Denormalize(ref double[] x)
        {
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = Tools.Denormalize(x[i]);
            }
        }
    }
}