我发现自己有一个案例,相当于floor $ 1/0正在执行.
?> 1/0
Infinity
Run Code Online (Sandbox Code Playgroud)
这是正常的行为,据我理解,但,当Infinity是floor"d或ceiling" d
?> floor $ 1/0
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
Run Code Online (Sandbox Code Playgroud)
而不是失败,而是产生了这么大的数字.为什么?
也许更重要的是,在应用其他功能之前,如何在不使用过滤器的情况下将其与非故障结果区分开来?
我在配置文件中定义了一个切片参数:
max_items = 10
Run Code Online (Sandbox Code Playgroud)
我的类根据此参数切片列表:
items=l[:config.max_itmes]
Run Code Online (Sandbox Code Playgroud)
什么时候max_items = 0,我想要取出所有物品l.快速而肮脏的方式是:
config.max_items=config.max_items if config.max_items>0 else 1e7
Run Code Online (Sandbox Code Playgroud)
假设会有更少的1e7项目.但是,我不喜欢使用魔术数字.是否有更多的Pythonic方式,如无穷大整数常量?
问题是关于在C++中为double数据类型建模无穷大.我需要在头文件中,所以我们不能使用像numeric_limits.
是否有定义的常量代表最大值?
在做项目euler#2的更多ruby方式中,部分代码是
while((v = fib(i)) < 4_000_000)
s+=v if v%2==0
i+=1
end
Run Code Online (Sandbox Code Playgroud)
有没有办法改变i += 1成更具功能性的编程风格结构?
我能想到的最好的是
Float::MAX.to_i.times do |i|
v = fib(i)
break unless v < 4_000_000
s += v if v%2==0
end
Run Code Online (Sandbox Code Playgroud)
因为你不能调用.times浮点数.
显然,infinity和NaN不是JSON规范的一部分,所以这个PHP代码:
$numbers = array();
$numbers ['positive_infinity'] = +INF;
$numbers ['negative_infinity'] = -INF;
$numbers ['not_a_number'] = NAN;
$array_print = print_r ($numbers, true);
$array_json = json_encode ($numbers);
echo "\nprint_r(): $array_print";
echo "\njson_encode(): $array_json";
Run Code Online (Sandbox Code Playgroud)
产生这个:
PHP Warning: json_encode(): double INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
PHP Warning: json_encode(): double -INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
PHP Warning: json_encode(): double NAN does not conform …Run Code Online (Sandbox Code Playgroud) 我有JavaScript对象说:
var a = {b: Infinity, c: 10};
Run Code Online (Sandbox Code Playgroud)
当我做
var b = JSON.stringify(a);
Run Code Online (Sandbox Code Playgroud)
它返回以下内容
b ="{"b":null,"c":10}";
JSON.stringify如何将对象转换为字符串?
我尝试过MDN解决方案.
function censor(key, value) {
if (value == Infinity) {
return "Infinity";
}
return value;
}
var b = JSON.stringify(a, censor);
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我必须返回字符串"Infinity" Infinity.如果我返回Infinity,它会再次将Infinity转换为null.
我该如何解决这个问题.
当我试图在R中表示无限日期时,我试图找出处理Postgresql表示能力的最佳方法,Infinity以及用于将数据传输到R -Infinity中的时间戳RPostgreSQL.在此过程中我发现了一些奇怪的行为.
我可以尝试以下列方式在负和正无穷大处创建日期:
? as.Date(-1/0, origin="1970-01-01")
[1] NA
? as.Date(1/0, origin="1970-01-01")
[1] NA
Run Code Online (Sandbox Code Playgroud)
它们似乎都是NA.然而,在比较它们时,似乎有一种理解认为一种比另一种更小.
? as.Date(-1/0, origin="1970-01-01") < as.Date(1/0, origin="1970-01-01")
[1] TRUE
? as.Date(-1/0, origin="1970-01-01") > as.Date(1/0, origin="1970-01-01")
[1] FALSE
? as.Date(1/0, origin="1970-01-01") > as.Date("1970-01-01")
[1] TRUE
? as.Date(1/0, origin="1970-01-01") < as.Date("1970-01-01")
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
如果他们都转换为NA,R如何知道差异?
我正在尝试创建一个等于Inf + Inf*j的复数无穷大,其中j是复数变量.当我这样做:
#include <complex>
#include <limits>
using std;
...
complex<double> attempt1 =
complex<double>( numeric_limits<double>::infinity(),
numeric_limits<double>::infinity() );
Run Code Online (Sandbox Code Playgroud)
返回复数(NaN + Inf*j).
但
complex<double> attempt2 =
complex<double>( numeric_limits<double>::infinity() );
Run Code Online (Sandbox Code Playgroud)
返回复数(Inf + 0*j).
另外:
complex<double> attempt_at_imag_inf =
complex<double>(any_value_here, numeric_limits<double>::infinity());
Run Code Online (Sandbox Code Playgroud)
返回复数(NaN + Inf*j).
有谁知道这里发生了什么?任何时候我试图为想象的部分提供信息,那么NaN就写在真实的部分上.
以上仅适用于支持NaN和Infinity的类型.我正在使用g ++ v4.6.1.我查看了numeric_limits标题,并没有迹象表明上面应该发生.
为了将上述内容置于上下文中,我实际上是在对numeric的一个部分特殊化中执行上述操作.非常感谢您考虑这个问题.
修改原始邮政
我正在提供一个完整但很短的程序来说明问题.我还提供了一些关于如何编译程序以生成结果的更多合格信息.
#include <iostream>
#include <complex>
#include <limits>
using namespace std;
int main(int argc, char* argv[])
{
complex<double> my_complex_inf =
complex<double>(numeric_limits<double>::infinity(),
numeric_limits<double>::infinity());
cout << "my_complex_inf = " << my_complex_inf << endl;
complex<double> attempt2 =
complex<double>( numeric_limits<double>::infinity() …Run Code Online (Sandbox Code Playgroud) 在我的一个模块中,我必须处理无限的概念.到目前为止,我一直在使用9**9**9正无穷大,这似乎运作良好,速度快,似乎是perl的内部用作无限.
但是,如果我的模块的用户决定使用其中一个大数字模块(例如use bigint;),然后他们使用inf或Math::BigInt->binf()表示无穷大,事情会有点冒险.
在某些地方它似乎工作正常,但在其他地方,比较应该是真的或应该是错误的最终错误的方式导致难以追踪错误.
我想支持无限的其他各种概念,它们可以使用普通的perl数和任意精度数.
但我也对性能表示担忧,因为我对无穷大的一些比较发生在紧密的内环中.显然,inffrom Math::BigInt会慢于9**9**9(由于在每次访问时调用绑定或重载方法).过去有没有人处理过这个问题?如果是这样,你的解决方案是什么?
我已经考虑过将自己的常量用于无穷大,定义如下:
use constant INF => if_any_bignum_modules_loaded()
? Math::BigInt->binf
: 9**9**9;
Run Code Online (Sandbox Code Playgroud)
然后向我的模块添加警告,首先应该加载任何bignum模块.这听起来合情合理吗?是否有可靠的实施if_any_bignum...,或者我应该自己推出?
GLSL是否有+/-无穷大或NaN的任何预定义常数?我这样做是一种解决方法,但我想知道是否有一种更清洁的方式:
// GLSL FRAGMENT SHADER
#version 410
<snip>
const float infinity = 1. / 0.;
void main ()
{
<snip>
}
Run Code Online (Sandbox Code Playgroud)
我知道这个isinf函数,但我需要为变量赋予无穷大,这对我没有帮助.