-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0d129b
commit 775be57
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const sine_radians = require("./sine_radians.js"); | ||
|
||
/** | ||
* @param {String} n - radians | ||
* @returns {String} sine of n | ||
*/ | ||
function sine() { | ||
const n = arguments[0]; | ||
const options = arguments[1]; | ||
return sine_radians(n, options); | ||
} | ||
|
||
module.exports = sine; | ||
module.exports.default = sine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use strict"; | ||
|
||
const add = require("./add.js"); | ||
const is_zero = require("./is_zero.js"); | ||
const square = require("./square.js"); | ||
const multiply_rational = require("./multiply_rational.js"); | ||
const divide = require("./divide.js"); | ||
|
||
function sine_radians(n, { steps = 100, max_decimal_digits = 100 } = {}) { | ||
if (is_zero(n)) return "0"; | ||
|
||
let sign = "-"; | ||
let result = n; | ||
let imax = steps; | ||
let nsquare = square(n); | ||
let numerator = n; | ||
let denominator = "1"; | ||
let f1; | ||
let f2 = "1"; | ||
for (let i = 0; i < imax; i++) { | ||
f1 = add(f2, "1"); | ||
f2 = add(f1, "1"); | ||
|
||
// same as increasing the power by 2 | ||
numerator = multiply_rational([numerator, nsquare], { max_decimal_digits }); | ||
denominator = multiply_rational([denominator, f1, f2], { max_decimal_digits }); | ||
const diff = divide(numerator, denominator, { max_decimal_digits }); | ||
// console.log({ f1, f2, sign, numerator, denominator, diff }); | ||
|
||
result = add(result, sign + diff); | ||
|
||
sign = sign === "-" ? "+" : "-"; | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = sine_radians; | ||
module.exports.default = sine_radians; |