首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在trig函数中,我如何在所有数字之后添加"deg“?

在trig函数中,我如何在所有数字之后添加"deg“?
EN

Stack Overflow用户
提问于 2022-01-13 18:00:19
回答 1查看 213关注 0票数 0

我使用的是Node.js和mathjs包。Mathjs在其trig函数中使用弧度。我想为我正在制作的计算器包括一个学位模式。如何在字符串中任何sin/cos/tan函数中的每个数字后面添加"deg“?这样,数学就会用度来计算,这样它就能工作了。

例: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())计算完整的表达式。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-13 18:08:35

您可以简单地用使用度的版本替换trig函数实现.

看看MathJS文档中的角度配置示例。您可以使用math.import函数为sincos等提供自己的函数实现,并且可以通过首先保存对它们的引用来引用原始函数。

以下是链接文档中的完整示例,作为可运行的片段(版权代码: 2013-2021 jos de jong):

代码语言:javascript
运行
复制
  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)
  }
代码语言:javascript
运行
复制
<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>

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

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

那是胡说。这并不是假设。这个例子就像你说的那样使用math.evaluate。它应该能完全按照你的需要工作。它的工作方式只是新的、被替换的函数在决定如何解释输入时引用了config.angles值(或者在弧形函数的情况下,指的是在返回输入之前转换结果的内容)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70701073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档