英文:
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)
)
更新:
根据文档:
> 常量 inf
和 nan
在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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论