Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Exp, Log & Pow  (Read 1407 times)
0 Members and 1 Guest are viewing this topic.
Offline Roquen
« Posted 2011-05-17 10:15:38 »

Sorry.  32-bit floats only.

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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
public final class FloatMath
{
  private FloatMath() {}
 
  // 3rd order: 14 good bits
  /**
  private static final float EXP2_0 = 0xf.ff8fcp-4f;
  private static final float EXP2_1 = 0xb.24b09p-4f;
  private static final float EXP2_2 = 0x3.96e3dp-4f;
  private static final float EXP2_3 = 0x1.446bap-4f;
  */

 
  // 4th order: 17 good bits
  /**
  private static final float EXP2_0 = 0x1.00002cp0f;
  private static final float EXP2_1 = 0x1.62d166p-1f;
  private static final float EXP2_2 = 0x1.ee798ap-3f;
  private static final float EXP2_3 = 0x1.aa13fp-5f;
  private static final float EXP2_4 = 0x1.bb7cd4p-7f;
  */


 
  // 5th order: 21 good bits
  private static final float EXP2_0 = 0xf.ffffep-4f;
  private static final float EXP2_1 = 0xb.1729ap-4f;
  private static final float EXP2_2 = 0x3.d79c1p-4f;
  private static final float EXP2_3 = 0xe.4d4cp-8f;
  private static final float EXP2_4 = 0x2.4a14p-8f;
  private static final float EXP2_5 = 0x7.c45p-12f;
 
 
  /**
   * Calculates: 2<sup>x</sup>
   */

 
  public static final float exp2(float x)
  {
    int   a = (int)x;
    float b = x-a;
    float r = 0;
   
  //r = b*(r + EXP2_6);
    r = b*(r + EXP2_5);
    r = b*(r + EXP2_4);
    r = b*(r + EXP2_3);
    r = b*(r + EXP2_2);
    r = b*(r + EXP2_1);
    r =   (r + EXP2_0);

    return exp2(a)*r;
  }
 
 
  // Result accurate to 22 bits (on primary range).
  private static final float LOG2_1 = 0x1.71547ap1f;
  private static final float LOG2_2 = 0x1.ec554ep-1f;
  private static final float LOG2_3 = 0x1.310a2cp-1f;
 
 
  /**
   * Calculates: Log<sub>2</sub> x
   * <p>
   * Accuracy: 22 bits (in normal range)
   */

 
  public static final float log2(float x)
  {
    int   i = Float.floatToRawIntBits(x);
    int   e = ((i >>> 23) & 0xff)-127;
    float y = Float.intBitsToFloat(i - (e<<23));
    float r = (y-sqrtOf2f)/(y+sqrtOf2f);
    float b = r * r;
    float a;
   
    // Approximate via ArcTanh:
    a = r*(LOG2_1 + b*(LOG2_2 + b*LOG2_3));
   
    return 0.5f + e + a;
  }
 
 
  // change to pre-calculated correctly round hex constant
  private static final float oneOverLog2 = (float)(1.0/Math.log(2));
 
  /**
   * Calculates: e<sup>x</sup>
   * <p>
   */

  public static final float exp(float x)
  {
    return exp2(x * oneOverLog2);
  }
 
  /**
   * Calculates: x<sup>y</sup>
   */

  public static final float powf(float x, float y)
  {
    return exp2(y * log2(x));
  }

  /**
   * Calculates: x<sup>n</sup>
   */

 
  public static final float pow(float x, int n)
  {
    float r = 1.f;
    int   e = n;
   
    if (n < 0)
      e = -n;

    do {
      if ((e & 1) != 0) r *= x;
     
      e >>>= 1;
     
      if (e != 0) {
        x *= x;
        continue;
      }
     
      if (n >= 0)
        return r;
     
      return 1/r;
     
    } while(true);
  }
 
  // hastings style
  private static final float sqrtOf2f = (float)Math.sqrt(2);
 

 
  // TODO: change to precalculated exact constant
  private static final float logOf2 = (float)Math.log(2);
 
  /**
   * Calculates: Log x
   */

  public static final float log(float x)
  {
    return log2(x) * logOf2;
  }
 
  // Log[2]/Log[10]
  private static final float log10Convert = 0x1.344136p-2f;
 
  /**
   * Calculates: Log<sub>10</sub> x
   */

  public static final float log10(float x)
  {
    return log2(x) * log10Convert;
  }
 
  /**
   * Calculates: 2<sup>x</sup> for inputs on [-127, 128].
   * <p>
   * The result is exact for valid input values,
   * otherwise the result is undefined.
   */

  public static final float exp2(int x)
  {
    return Float.intBitsToFloat((x+127)<<23);
  }
 

}
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-05-17 11:32:00 »

It's a shame that JS prettyfier can't handle multi-line comments.

Besides that, why not post it in the code-snippet section?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Roquen
« Reply #2 - Posted 2011-05-17 11:48:59 »

Humm..good question.  I guess I'm not sure where the "line" is between here and there. (edited code so it colorifies better)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (34 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (74 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!