gnuplot:如何与NaN进行比较? [英] gnuplot: How to compare to NaN?
问题描述
这里有一些关于此的主题(使用javascript,c ++等),但尚未找到适合gnuplot的主题.
There are several topics here about this (in javascript, c++,...), but haven't yet found one for gnuplot.
基本上,我想在标签中将"nan"替换为"---".
Basically, I want to replace "nan" with "---" in labels.
我知道我可以设置 NaN ="---"
,但是 NaN
的行为就像一个字符串,需要修改其他代码.
I know that I can set NaN = "---"
but then NaN
behaves like a string and other code needs to be adapted.
### How to compare to NaN?
NaN = GPVAL_NaN # default value for NaN
a = NaN
print (a==NaN ? "---" : sprintf("%g",a))
print (a==NaN ? sprintf("%g",a) : "---")
print (a!=NaN ? "---" : sprintf("%g",a))
print (a!=NaN ? sprintf("%g",a) : "---")
a = 123
print (a==NaN ? "---" : sprintf("%g",a))
print (a==NaN ? sprintf("%g",a) : "---")
print (a!=NaN ? "---" : sprintf("%g",a))
print (a!=NaN ? sprintf("%g",a) : "---")
### end of code
结果:
NaN
---
NaN
---
123
---
123
---
因此,没有一种组合可以完成这项工作.
So, none of the combination does the job.
最后,我发现以下内容似乎可行.
Finally, I found that the following seems to work.
print (a/a == 1 ? sprintf("%g",a) : "---")
我的问题:
a/a
是否总是精确地为 1
,或者在特殊情况下是否可能存在舍入错误?是这样做的方式"还是我忽略了任何事情?
Will a/a
always be exactly 1
or could there potentially be rounding errors in special cases?
Is this "the" way how to do it or did I overlook anything?
推荐答案
只是为了不让这个问题出现而没有答案...
just for the sake of not letting this question appearing unanswered...
一种比较变量 a
是否为 NaN
(至少在gnuplot中)(并至少对其进行处理)的最简单方法:
Probably, the easiest way to compare if a variable a
is NaN
or not (at least in gnuplot) and do something with it:
print (a == a ? sprintf("%g",a) : "---")
在SO上对某些编程语言进行讨论之后,人们开始辩论 NaN == NaN
或 NaN!= NaN
或两者都不是.
Following some discussions on some programming languages here on SO, people are debating about NaN==NaN
or NaN!=NaN
or neither of them.
如果与数据中的字符串"NaN"
进行比较,请参见上面的@Christoph注释.
And in case of comparison to the string "NaN"
in data: see @Christoph's comment above.
这篇关于gnuplot:如何与NaN进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!