如何在三角函数中的所有数字后添加 "deg"?

How can I add "deg" after all numbers in trig functions?

我正在使用 Node.js 和 mathjs 包。 Mathjs 在其三角函数中使用弧度。我想为我正在制作的计算器添加一个度数模式。如何在字符串中任何 sin/cos/tan 函数中的每个数字后添加“deg”?这样,mathjs 将使用度数来计算它,这样它就可以工作了。

示例:5*(2+3)/sin(4+(3-2)) 应变为 5*(2+3)/sin(4deg+(3deg-2deg)) 但是:5*(2+3)/(4+(3-2)) 不应更改。

我到处都找过了:正则表达式、reddit、google,我一无所获。我最接近的是一个正则表达式,当 sin 函数 (/sin\(.*?\)/) 中有括号时,它会中断。

编辑:是的,我看过角度配置示例。不,这并没有解决我的问题:它假设用户正在实现一个单一的值来计算正弦值,我正在尝试计算具有度数模式的完整表达式 (mathjs.evaluate())

您可以改为简单地将三角函数实现替换为使用度数的版本...

看看angle configuration example in the MathJS docs。您可以使用math.import函数为sincos等函数提供自己的函数实现,您可以通过先保存对它们的引用来引用原始的。

这是链接文档中作为可运行片段的完整示例(版权所有 © 2013-2021 jos de jong):

  let replacements = {}

  // our extended configuration options
  const config = {
    angles: 'deg' // 'rad', 'deg', 'grad'
  }

  // create trigonometric functions replacing the input depending on angle config
  const fns1 = ['sin', 'cos', 'tan', 'sec', 'cot', 'csc']
  fns1.forEach(function(name) {
    const fn = math[name] // the original function

    const fnNumber = function (x) {
      // convert from configured type of angles to radians
      switch (config.angles) {
        case 'deg':
          return fn(x / 360 * 2 * Math.PI)
        case 'grad':
          return fn(x / 400 * 2 * Math.PI)
        default:
          return fn(x)
      }
    }

    // create a typed-function which check the input types
    replacements[name] = math.typed(name, {
      'number': fnNumber,
      'Array | Matrix': function (x) {
        return math.map(x, fnNumber)
      }
    })
  })

  // create trigonometric functions replacing the output depending on angle config
  const fns2 = ['asin', 'acos', 'atan', 'atan2', 'acot', 'acsc', 'asec']
  fns2.forEach(function(name) {
    const fn = math[name] // the original function

    const fnNumber = function (x) {
      const result = fn(x)

      if (typeof result === 'number') {
        // convert to radians to configured type of angles
        switch(config.angles) {
          case 'deg':  return result / 2 / Math.PI * 360
          case 'grad': return result / 2 / Math.PI * 400
          default: return result
        }
      }

      return result
    }

    // create a typed-function which check the input types
    replacements[name] = math.typed(name, {
      'number': fnNumber,
      'Array | Matrix': function (x) {
        return math.map(x, fnNumber)
      }
    })
  })

  // import all replacements into math.js, override existing trigonometric functions
  math.import(replacements, {override: true})

  // pointers to the input elements
  const expression = document.getElementById('expression')
  const evaluate   = document.getElementById('evaluate')
  const result     = document.getElementById('result')
  const angles     = document.getElementById('angles')

  // attach event handlers for select box and button
  angles.onchange = function () {
    config.angles = this.value
    config.angles = this.value
  }
  evaluate.onclick = function () {
    result.innerHTML = math.evaluate(expression.value)
  }
<script src="https://unpkg.com/mathjs@10.0.2/lib/browser/math.js"></script>

<table>
  <tr>
    <th>Angles</th>
    <td>
      <select id="angles">
        <option value="deg">deg</option>
        <option value="grad">grad</option>
        <option value="rad">rad</option>
      </select>
    </td>
  </tr>
  <tr>
    <th>Expression</th>
    <td>
      <input id="expression" type="text" value="sin(45)" />
      <input id="evaluate" type="button" value="Evaluate">
    </td>
  </tr>
  <tr>
    <th>Result</th>
    <td id="result"></td>
  </tr>
</table>

编辑:回复问题中的编辑:

Edit: Yes I have looked at the angle configuration example. No, that doesn't solve my problem: It assumes the user is implementing a single value to calculate the sine of, I am trying to calculate full expressions with a degree mode (mathjs.evaluate())

事实并非如此。它不假设。正如您所说,该示例使用 math.evaluate 。它应该完全按照您的需要工作。它的工作方式只是新的、替换的函数在决定解释输入时引用 config.angles 值(或者在 arcus 函数的情况下,在返回结果之前转换结果)。