-
Notifications
You must be signed in to change notification settings - Fork 18
/
bcmath.js
233 lines (186 loc) · 7.78 KB
/
bcmath.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
var libbcmath = require("./libbcmath")
/**
* PHP Implementation of the libbcmath functions
*
* Designed to replicate the PHP functions exactly.
* Also includes new function: bcround
*/
/**
* bcadd - Add two arbitrary precision numbers
* Sums left_operand and right_operand.
*
* @param string left_operand The left operand, as a string
* @param string right_operand The right operand, as a string.
* @param int [scale] The optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global scale for all functions by using bcscale()
* @return string
*/
function bcadd(left_operand, right_operand, scale) {
var first, second, result;
if (typeof(scale) == 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
// create objects
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
result = libbcmath.bc_init_num();
first = libbcmath.php_str2num(left_operand.toString());
second = libbcmath.php_str2num(right_operand.toString());
// normalize arguments to same scale.
if (first.n_scale > second.n_scale) second.setScale(first.n_scale);
if (second.n_scale > first.n_scale) first.setScale(second.n_scale);
result = libbcmath.bc_add(first, second, scale);
if (result.n_scale > scale) {
result.n_scale = scale;
}
return result.toString();
}
/**
* bcsub - Subtract one arbitrary precision number from another
* Returns difference between the left operand and the right operand.
*
* @param string left_operand The left operand, as a string
* @param string right_operand The right operand, as a string.
* @param int [scale] The optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global scale for all functions by using bcscale()
* @return string
*/
function bcsub(left_operand, right_operand, scale) {
var first, second, result;
if (typeof(scale) == 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
// create objects
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
result = libbcmath.bc_init_num();
first = libbcmath.php_str2num(left_operand.toString());
second = libbcmath.php_str2num(right_operand.toString());
// normalize arguments to same scale.
if (first.n_scale > second.n_scale) second.setScale(first.n_scale);
if (second.n_scale > first.n_scale) first.setScale(second.n_scale);
result = libbcmath.bc_sub(first, second, scale);
if (result.n_scale > scale) {
result.n_scale = scale;
}
return result.toString();
}
/**
* bccomp - Compare two arbitrary precision numers
*
* @param string left_operand The left operand, as a string
* @param string right_operand The right operand, as a string.
* @param int [scale] The optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global scale for all functions by using bcscale()
* @return int 0: Left/Right are equal, 1 if left > right, -1 otherwise
*/
function bccomp(left_operand, right_operand, scale) {
var first, second; //bc_num
if (typeof(scale) == 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
first = libbcmath.bc_str2num(left_operand.toString(), scale); // note bc_ not php_str2num
second = libbcmath.bc_str2num(right_operand.toString(), scale); // note bc_ not php_str2num
return libbcmath.bc_compare(first, second, scale);
}
/**
* bcscale - Set default scale parameter for all bc math functions
* @param int scale The scale factor (0 to infinate)
* @return bool
*/
function bcscale(scale) {
scale = parseInt(scale, 10);
if (isNaN(scale)) {
return false;
}
if (scale < 0) {
return false;
}
libbcmath.scale = scale;
return true;
}
/**
* bcdiv - Divide two arbitrary precision numbers
*
* @param string left_operand The left operand, as a string
* @param string right_operand The right operand, as a string.
* @param int [scale] The optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global scale for all functions by using bcscale()
* @return string The result as a string
*/
function bcdiv(left_operand, right_operand, scale) {
var first, second, result;
if (typeof(scale) == 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
// create objects
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
result = libbcmath.bc_init_num();
first = libbcmath.php_str2num(left_operand.toString());
second = libbcmath.php_str2num(right_operand.toString());
// normalize arguments to same scale.
if (first.n_scale > second.n_scale) second.setScale(first.n_scale);
if (second.n_scale > first.n_scale) first.setScale(second.n_scale);
result = libbcmath.bc_divide(first, second, scale);
if (result === -1) {
// error
throw new Error(11, "(BC) Division by zero");
}
if (result.n_scale > scale) {
result.n_scale = scale;
}
return result.toString();
}
/**
* bcdiv - Multiply two arbitrary precision number
*
* @param string left_operand The left operand, as a string
* @param string right_operand The right operand, as a string.
* @param int [scale] The optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global scale for all functions by using bcscale()
* @return string The result as a string
*/
function bcmul(left_operand, right_operand, scale) {
var first, second, result;
if (typeof(scale) == 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
// create objects
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
result = libbcmath.bc_init_num();
first = libbcmath.php_str2num(left_operand.toString());
second = libbcmath.php_str2num(right_operand.toString());
// normalize arguments to same scale.
if (first.n_scale > second.n_scale) second.setScale(first.n_scale);
if (second.n_scale > first.n_scale) first.setScale(second.n_scale);
result = libbcmath.bc_multiply(first, second, scale);
if (result.n_scale > scale) {
result.n_scale = scale;
}
return result.toString();
}
/**
* bcround - Returns the rounded value of [val] to the specified [precision] (number of digits after the decimal point).
* [precision] can also be a negative or zero (default)
* Note: uses "round up and away from zero" method (ie -1.5 > -2, 1.5 > 2 where .5 always goes to 1 (or 0.5 to -1) etc
*
* @param string val The value to round (accept in virtually any format)
* @param int precision The optional number of digits to round-to
* @return string In exact decimal places of precision (ie bcround('1.2222', 2) == '1.22' or bcround('1', 4) == '1.0000' )
*/
function bcround(val, precision) {
var x, r;
x = '0.' + Array(precision+1).join('0') + '5';
if (val.toString().substring(0, 1) == '-') {
x = '-' + x;
}
r = bcadd(val, x, precision);
return r;
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
module.exports = { bcadd, bcsub, bccomp, bcscale, bcdiv, bcmul, bcround }