Small traps for numerical calculation in

Source: Internet
Author: User

In as, we use the is operator to determine the data type.

But what are the results of the uint, Int, and number values?

Let's do a small test:

VaR U: uint = 3; <br/> trace (U is uint); // true <br/> trace (U is int ); // true <br/> trace (U is number); // true </P> <p> var I: Int = 4; <br/> trace (I is uint); // true <br/> trace (I is int); // true <br/> trace (I is number ); // true </P> <p> var N: Number = 5; <br/> trace (N is uint ); // true <br/> trace (N is int); // true <br/> trace (N is number ); // true </P> <p> var number: Number = 6.6; <br/> trace (number is uint ); // false <br/> trace (number is int); // false <br/> trace (number is number); // true <br/>

 

It can be seen that the is operator does not strictly judge the data of the three data types according to the originally defined data types, and is true if it is convertible.

 

 

 

We use the isnan () function to check whether a mathematical expression is successfully calculated as a number. If the value isNaN(Not a number), returntrue。

The Nan value is a special member of the number data type, but is not a member of the Int or uint data type. It is used to represent a "non-number" value.

By the way, we will introduce two as top-level constants-infinity and infinity, which are easily overlooked:

-Infinity: NegativeInfinity. The value of this constant correspondsNumber.NEGATIVE_INFINITYSame.

Infinity: positiveInfinity. The value of this constant correspondsNumber.POSITIVE_INFINITYSame.

trace(0 / 0); // NaN<br />trace(7 / 0); // Infinity<br />trace(-7 / 0); // -Infinity<br />

 

Next, we will continue a set of tests:

var n:Number = 6.6;<br />trace(!isNaN(n)); //true<br />trace(!isNaN(n) && n != Infinity && n != -Infinity) //true<br />var nn:Number = 6.6 / 0;<br />trace(nn); //Infinity<br />trace(!isNaN(nn)); //true<br />trace(!isNaN(nn) && nn != Infinity && nn != -Infinity) //false<br />var nan:Number = 0 / 0;<br />trace(nan); //NaN<br />trace(nan is Number); //true<br />trace(!isNaN(nan)); //false


It can be seen that we cannot simply use the is number when checking whether a variable is a valid number, but use the isnan () function.

When! Isnan (value) returns true and is not equal to +/-infinity, which indicates that it is a valid number value.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.