首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Bash中使用三角函数

在Bash中使用三角函数
EN

Stack Overflow用户
提问于 2020-05-11 14:49:29
回答 1查看 558关注 0票数 0

我正在尝试编写一个bash脚本来计算风校正角,但是每当我运行脚本的trig部分时都会遇到错误。

代码语言:javascript
复制
line 32: sin(-14): syntax error in expression (error token is "(-14)"

我知道WCA的完整公式还没有出现,但我想在继续之前解决这个问题。脚本发布在下面。如果有任何其他细节需要,我很乐意为您效劳。干杯

代码语言:javascript
复制
#!/bin/bash

# This script is to be used in Cross Country calculations

echo 'What is True Course?'

read true_course

echo 'What is True Airspeed?'

read true_airspeed

echo 'What is Wind Direction?'

read wind_direction

echo 'What is Wind Speed?'

read wind_speed

# Formula for WCA is below
# WCA = sin-1 (sin(WD-TC)*WV/TAS)

course_adjustment=$(($wind_direction-$true_course))

course_adjustment2=$(($course_adjustment*$wind_speed/$true_airspeed))

wca=$((sin($course_adjustment2)))

echo 'Course adjustment is' $course_adjustment
echo 'Course adjustment2 is' $course_adjustment2

# Formula for GS
# GS = SqRt(TAS2 + WV2 - 2*TAS*WV*cos(WD-TC-WCA))

echo 'Calculations complete!'

echo 'True Course is' $true_course

echo 'True Airspeed is' $true_airspeed

echo 'Wind Direction is' $wind_direction

echo 'Wind Speed is' $wind_speed
EN

回答 1

Stack Overflow用户

发布于 2020-05-11 15:53:29

Bash's arithmetic support is limited to integers,所以你不能在纯Bash中做三角运算。

此外,Bash的算术上下文不支持任何函数概念,更不用说trig函数了,这就是为什么您会看到听起来奇怪的错误。$((sin($course_adjustment2)))等同于$(($sin($course_adjustment2))) (算术上下文中的变量不需要$,所以它寻找$sin变量)。

在Bash中进行浮点运算的一般方法(除了not using Bash at all,为什么不是Python呢?)就像肖恩建议的那样,和bc在一起。

代码语言:javascript
复制
# note the -l argument; s[ine] and c[osine] arguments are in radians 
$ bc -l <<<"scale=4; s(1); c(1)"
.8414
.5404
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61723795

复制
相关文章

相似问题

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