infinity

How do I create or test for NaN or infinity in Perl?

心已入冬 提交于 2019-11-27 23:29:22
How do I create or test for NaN or infinite values in Perl? print "Is NaN\n" if $a eq 'nan'; print "Is Inf\n" if $a eq 'inf' or $a eq '-inf'; EDIT : Fixed for negative infinity. ysth Here's a fairly reliable way: my $inf = 9**9**9; my $neginf = -9**9**9; my $nan = -sin(9**9**9); sub isinf { $_[0]==9**9**9 || $_[0]==-9**9**9 } sub isnan { ! defined( $_[0] <=> 9**9**9 ) } # useful for detecting negative zero sub signbit { substr( sprintf( '%g', $_[0] ), 0, 1 ) eq '-' } for my $num ( $inf, $neginf, $nan ) { printf("%s:\tisinf: %d,\tisnan: %d,\tsignbit: %d\n", $num, isinf($num), isnan($num),

How to generate NaN, -Infinity and +Infinity in ANSI C?

风格不统一 提交于 2019-11-27 19:36:21
I use ANSI C89 (not C++), and I want to generate NaN, -Infinity and +Infinity. Is there any standard way (eg. standard macro)? Or is there any platform and compiler independent way to generate these numbers? float f = 0.0 / 0.0; // Is f ALWAYS in any platform is NaN? There is in C99, but not in previous standards AFAIK. In C99, you'll have NAN and INFINITY macros. From "Mathematics <math.h> " (§7.12) section The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available; ... If you're stuck with ANSI C89, you're out of luck. See C-FAQ

In Haskell, is there infinity :: Num a => a?

好久不见. 提交于 2019-11-27 17:13:13
问题 I'm trying to implement a data structure where if I had the use of infinity for numerical comparison purposes, it would simply things greatly. Note this isn't maxBound/minBound, because a value can be <= maxbound, but all values would be < infinity. No hope? 回答1: Maybe you want a Maybe type? data Infinite a = Infinite | Only a then write a Num instance for Num a => Infinite a, with the numeric rules you need. 回答2: Well how about that! It turns out if you just type 1/0 it returns Infinity ! On

Python Infinity - Any caveats?

落花浮王杯 提交于 2019-11-27 17:03:47
So Python has positive and negative infinity: float("inf"), float("-inf") This just seems like the type of feature that has to have some caveat. Is there anything I should be aware of? You can still get not-a-number (NaN) values from simple arithmetic involving inf : >>> 0 * float("inf") nan Note that you will normally not get an inf value through usual arithmetic calculations: >>> 2.0**2 4.0 >>> _**2 16.0 >>> _**2 256.0 >>> _**2 65536.0 >>> _**2 4294967296.0 >>> _**2 1.8446744073709552e+19 >>> _**2 3.4028236692093846e+38 >>> _**2 1.157920892373162e+77 >>> _**2 1.3407807929942597e+154 >>> _**2

Infinity symbol with HTML

被刻印的时光 ゝ 提交于 2019-11-27 11:23:52
问题 How can I display an infinity symbol (like the one in the picture) using HTML? 回答1: Use the HTML entity ∞ or ∞ . 回答2: You can use the following: literal: ∞ (if the encoding you use can encode it — UTF-8 can, for example) character reference: ∞ (decimal), ∞ (hexadecimal) entity reference: ∞ But whether it is displayed correctly does also depend on the font the text is displayed with. 回答3: ∞ This does not require a HTML entity if you are using a modern encoding (such as UTF-8). And if you're

How can I represent an infinite number in Python?

一世执手 提交于 2019-11-27 09:58:06
How can I represent an infinite number in python? No matter which number you enter in the program, no number should be greater than this representation of infinity. In Python, you can do: test = float("inf") In Python 3.5, you can do: import math test = math.inf And then: test > 1 test > 10000 test > x Will always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number"). Additionally (Python 2.x ONLY), in a comparison to Ellipsis , float(inf) is lesser, e.g: float('inf') < Ellipsis would return true. Since Python 3.5 you can use math.inf : >>> import math >>>

JSON.stringify converting Infinity to null

走远了吗. 提交于 2019-11-27 09:04:10
I have JavaScript Object say: var a = {b: Infinity, c: 10}; When I do var b = JSON.stringify(a); it returns the following b = "{"b":null, "c":10}"; How is the JSON.stringify converts the object to strings? I tried MDN Solution . function censor(key, value) { if (value == Infinity) { return "Infinity"; } return value; } var b = JSON.stringify(a, censor); But in this case I have to return the string "Infinity" not Infinity . If I return Infinity it again converts Infinity to null. How do I solve this problem. Like the other answers stated, Infintity is not part of the values JSON can store as

Why does this method return double.PositiveInfinity not DivideByZeroException?

一个人想着一个人 提交于 2019-11-27 07:38:41
问题 I ran the following snippet in the VS2015 C# interactive and got some very weird behavior. > double divide(double a, double b) . { . try . { . return a / b; . } . catch (DivideByZeroException exception) . { . throw new ArgumentException("Argument b must be non zero.", exception); . } . } > divide(3,0) Infinity > 3 / 0 (1,1): error CS0020: Division by constant zero > var b = 0; > 3 / b Attempted to divide by zero. > Why did the method return infinity while 3 / 0 threw an error and 3 / b threw

PHP: How to encode infinity or NaN numbers to JSON?

半腔热情 提交于 2019-11-27 07:35:48
问题 Apparently, infinity and NaN are not a part of JSON specification, so this PHP code: $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"; Produces this: PHP Warning: json_encode(): double INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on

How to implement infinity in Java?

天涯浪子 提交于 2019-11-27 06:15:00
Does Java have anything to represent infinity for every numerical data type? How is it implemented such that I can do mathematical operations with it? E.g. int myInf = infinity; //However it is done myInf + 5; //returns infinity myInf*(-1); //returns negative infinity I have tried using very large numbers, but I want a proper, easy solution. double supports Infinity double inf = Double.POSITIVE_INFINITY; System.out.println(inf + 5); System.out.println(inf - inf); // same as Double.NaN System.out.println(inf * -1); // same as Double.NEGATIVE_INFINITY prints Infinity NaN -Infinity note: Infinity