amath  1.8.5
Simple command line calculator
cbrt.c File Reference

Cube root function. More...

#include "prim.h"
Include dependency graph for cbrt.c:

Go to the source code of this file.

Functions

double cbrt (double x)
 Cube root function. More...
 

Variables

static const unsigned B1 = 715094163
 
static const unsigned B2 = 696219795
 
static const double C = 5.42857142857142815906e-01
 
static const double D = -7.05306122448979611050e-01
 
static const double E = 1.41428571428571436819e+00
 
static const double F = 1.60714285714285720630e+00
 
static const double G = 3.57142857142857150787e-01
 

Detailed Description

Cube root function.

Definition in file cbrt.c.

Function Documentation

◆ cbrt()

double cbrt ( double  x)

Cube root function.

Definition at line 62 of file cbrt.c.

References B1, B2, C, D, E, F, and G.

Referenced by RealNumber::CubeRoot().

63 {
64  int32_t hx, lx, ht;
65  double r, s, t = 0.0, w;
66  uint32_t sign;
67 
68  GET_HIGH_WORD(hx, x);
69 
70  // sign = sign(x)
71  sign = hx & 0x80000000;
72  hx ^= sign;
73 
74  // cbrt(NaN,INF) is itself
75  if (hx >= 0x7FF00000)
76  {
77  return x + x;
78  }
79 
80  GET_LOW_WORD(lx, x);
81 
82  // cbrt(0) is itself
83  if ((hx | lx) == 0)
84  {
85  return x;
86  }
87 
88  // x <- |x|
89  SET_HIGH_WORD(x, hx);
90 
91  // rough cbrt to 5 bits
92  if (hx < 0x00100000) // subnormal number
93  {
94  SET_HIGH_WORD(t, 0x43500000); // set t= 2**54
95  t *= x;
96  GET_HIGH_WORD(ht, t);
97  SET_HIGH_WORD(t, ht / 3 + B2);
98  }
99  else
100  {
101  SET_HIGH_WORD(t, hx / 3 + B1);
102  }
103 
104  // new cbrt to 23 bits, may be implemented in single precision
105  r = t * t / x;
106  s = C + r * t;
107  t *= G + F / (s + E + D / s);
108 
109  // chopped to 20 bits and make it larger than cbrt(x)
110  SET_LOW_WORD(t, 0);
111  GET_HIGH_WORD(ht, t);
112  SET_HIGH_WORD(t, ht + 0x00000001);
113 
114  // one step newton iteration to 53 bits with error less than 0.667 ulps
115  s = t * t; // t*t is exact
116  r = x / s;
117  w = t + t;
118  r = (r - t) / (w + r); // r-s is exact
119  t = t + t * r;
120 
121  // retore the sign bit
122  GET_HIGH_WORD(ht, t);
123  SET_HIGH_WORD(t, ht | sign);
124 
125  return (t);
126 }
static const double D
Definition: cbrt.c:54
static const double E
Definition: cbrt.c:55
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
#define GET_LOW_WORD(i, d)
Get the less significant 32 bit int from a double.
Definition: prim.h:177
static const double F
Definition: cbrt.c:56
static const unsigned B2
Definition: cbrt.c:50
static const double G
Definition: cbrt.c:57
static const unsigned B1
Definition: cbrt.c:49
static const double C
Definition: cbrt.c:53
#define SET_LOW_WORD(d, v)
Set the less significant 32 bits of a double from an int.
Definition: prim.h:209
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:198
Here is the caller graph for this function:

Variable Documentation

◆ B1

const unsigned B1 = 715094163
static

Definition at line 49 of file cbrt.c.

Referenced by cbrt().

◆ B2

const unsigned B2 = 696219795
static

Definition at line 50 of file cbrt.c.

Referenced by cbrt().

◆ C

const double C = 5.42857142857142815906e-01
static

Definition at line 53 of file cbrt.c.

Referenced by cbrt().

◆ D

const double D = -7.05306122448979611050e-01
static

Definition at line 54 of file cbrt.c.

Referenced by cbrt().

◆ E

const double E = 1.41428571428571436819e+00
static

Definition at line 55 of file cbrt.c.

Referenced by cbrt().

◆ F

const double F = 1.60714285714285720630e+00
static

Definition at line 56 of file cbrt.c.

Referenced by cbrt().

◆ G

const double G = 3.57142857142857150787e-01
static

Definition at line 57 of file cbrt.c.

Referenced by cbrt().