区分带小数位的数字(双精度类型)和不带小数位的数字 - c++
我正在尝试用 C++ 实现一个简单的抽取算法。我有两个数组,例如 p
& q
,其中下标通过以下关系相互关联:
p[k] = q[0.5*k]
。这意味着以下序列应该有效:
p[0] = q[0]
p[1] = 0
p[2] = q[1]
p[3] = 0
p[4] = q[2]
依此类推...
请注意,p[k]
仅当结果为 (0.5*k)< 时才取值。 /code> 不包含小数位(或小数点为 0),并且不使用任何舍入等。
我的问题是: 有没有办法区分整数(没有小数位的数字)或者只有十进制的 0,例如 2.0)和 C++ 中带有小数位的数字,前提是两者都转换为 double?
例如。)2.0 是一个转换为 double 的整数。 2.1 是一个带小数位的数字。
例如。 2) * 0.9*2 应该将 0 放入数组 p 中,而 0.9*10 应该将 q[9] 放入数组 p 中。*
如果我使用语句 (int) (0.5*k)
,那么无论 k 的值如何,在每种情况下我都会得到一个整数。
编辑:上例中的 0.5 只是说明性的。它可以是任何数字,例如 2、2.5、0.9、0.95 等)
非常欢迎任何帮助,
谢谢,
斯里拉姆。
I am trying to implement a simple decimation algorithm in c++. I have two arrays, say p
& q
, where the subscripts are related to each other by the following relation:
p[k] = q[0.5*k]
. This means that the following sequence should hold valid:
p[0] = q[0]
p[1] = 0
p[2] = q[1]
p[3] = 0
p[4] = q[2]
and so on...
Please note that p[k]
takes on a value only and only when the result of (0.5*k)
contains no decimal places (or has 0 in decimal) and does not use any rounding off etc.
My question is: Is there a way to distinguish between an integer (a number with no decimal places or only 0 in decimal, say 2.0) and a number with decimal places in C++, provided both are cast to double?
eg.) 2.0 is an integer cast to double. 2.1 is a number with decimal places.
eg. 2) * 0.9*2 should put 0 into array p while 0.9*10 should put q[9] into array p.*
If I use the statement, (int) (0.5*k)
, then I end up with an integer in every case, irrespective of the value of k.
Edit: The 0.5 in the above case is only illustrative. It could be any number, say 2, 2.5, 0.9, 0.95 etc.)
Any help is most welcome,
Thanks,
Sriram.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设
k
是整数类型,您可以使用if (k % 2 == 0) ...
来检查k
是否可整除由二:这也可以使用三元运算符来表达:
Assuming
k
is of an integer type, you could useif (k % 2 == 0) ...
to check ifk
is divisible by two:This can also be expressed using the ternary operator:
假设 coef 可以是其他任何值,
Presuming that the coef can be anything else,
您想要执行的操作的简短语法可能是这样的:
定义整数,并在此上下文中定义整数。我很困惑!
您是否按照解释来理解差异 这里?
如果你想检测一个数字是否是整数,那么这可能会有所帮助:
The short syntax for what you want to do could be this:
Define whole number, and define integer in this context. I'm confused!
Are you taking about the difference as explained here?
If you want to detect whether a number is integer or not, then probably this may help:
k % 2 在这个线程的几个答案中。
然而,这对于回答OP的问题没有用。注意编辑:
“编辑:上述情况中的 0.5 只是说明性的。它可以是任何数字,例如 2、2.5、0.9、0.95 等)”
k % 2 仅有效,因为选择的值为 0.5。它不适用于任何其他值。
因此,除非我完全遗漏了一些东西,否则我能想到的最简单的方法如下:
从数字本身中减去数字的下限。如果结果> 0,它不是整数。
k % 2 is in a couple of answers in this thread.
However, this is not useful in answering the OP's question. Note the edit:
"Edit: The 0.5 in the above case is only illustrative. It could be any number, say 2, 2.5, 0.9, 0.95 etc.)"
k % 2 only works because the value chosen was 0.5. It won't hold true for any other values.
Therefore, unless I'm missing something entirely, the simplest approach I can think of is the following:
Subtract the floor of the number from the number itself. If the result is > 0, it is not an integer.
除非您的表达式会产生无理数,否则您可以使用 Boost。 Rational 代表您的索引。
Unless you have expressions that result in irrational numbers, you could use Boost.Rational to represent your indizes.
@Aix 对
k%2
的建议看起来可以与?:
运算符很好地结合起来:@Aix's suggestion of
k%2
looks like it'd combine nicely with the?:
operator: