在OpenSCAD中测试“inf”值。

huangapple go评论93阅读模式
英文:

Test for the "inf" value in OpenSCAD

问题

我对OpenSCAD相对不太熟悉,但我在软件开发方面有丰富的经验。我已经编写了以下的function

function GetSlope(angle)=sin(angle)/cos(angle);

当角度为90度或270度时,由于cos评估为0,因此function当然会返回inf,因此尝试除以0。这显然是可以预料的,但我需要在使用这个function时能够检测到这一点。OpenSCAD包含多个类型测试函数,但我找不到一个名为is_inf的函数或一个名为inf的常量来进行我的测试。是否有一种方法可以测试inf,还是我需要在function外部手动测试值?

英文:

I am relatively new to OpenSCAD, but I have plenty of experience in software development. I have written the following function:

function GetSlope(angle)=sin(angle)/cos(angle);

When the angle is 90° or 270° the function will, of course, return inf due to cos evaluating to 0, and therefore trying to divide by 0. This is obviously to be expected, but I need to be able to detect this when using the function. OpenSCAD contains several type-testing functions, but I could not find an is_inf function or a constant named inf to do my own test. Is there a way to test for inf, or do I need to manually test the value from outside the function?

答案1

得分: 1

你可以使用 1/0 作为你寻找的 inf 常量,虽然不太规范但可行。你也可以检查 cos(angle) == 0,尽管我认为这可能不是你想要的。

(也许你已经知道,但 sin(angle) / cos(angle) = tan(angle))

更新:

根据文档

> 常量 infnan 在OpenSCAD中不被支持作为数值常量,尽管你可以通过 'echo' 打印这样的计算结果。你可以通过以下方式定义具有这些值的变量: inf = 1e200 * 1e200; nan = 0 / 0; echo(inf,nan);

英文:

You can use 1/0 as the inf constant you are looking for, it's not very clean but it works. You could also check if cos(angle) == 0 although I don't think that's what you want.

(also you may already know it but sin(angle) / cos(angle) = tan(angle))

UPDATE :

according to the documentation :

> The constants inf and nan are not supported as numeric constants by OpenSCAD, even though you can compute numbers that are printed this way by 'echo'. You can define variables with these values by using: inf = 1e200 * 1e200;
nan = 0 / 0;
echo(inf,nan);

huangapple
  • 本文由 发表于 2023年5月15日 03:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定