String.valueof (Math.Abs) (New Random (). Nextint ())

Source: Internet
Author: User
Tags abs numeric value

String.valueof (Math.Abs) (New Random (). Nextint ())

Bad attempt to compute absolute value of signed 32-bit random integer

I don't know what the harm is.

Mccxj wrote Prowl wrote Mccxj wrote Math.Abs (integer.min_value) = = Integer.min_value


The bold text sentence, this expression is why

Why is there a negative value?

The value of int has a range (minimum-2147483648, maximum 2147483647), determined by the storage structure of the numeric value.

To add, the ABS calculation:
public static int abs (int a) {
Return (a < 0)? -A:A;
}
-A = ~a + 1
0x80000000 = ~0x80000000 + 1 = 0x7fffffff + 1 = 0x80000000

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.