How to determine negative number and positive number in Java?

Source: Internet
Author: User

This is Java code:

Package COM. test. day13.num;/*** @ author Bigbird * @ date 10:12:33 * @ action test to determine whether a number is positive or negative */public class numtest {public static Boolean positive (double F) {final Boolean pos0 [] = {true}; Final Boolean posn [] = {false, true}; If (F = 0.0) return true; while (true) {// if F is in]-1.0; 1.0 [, multiply it by 2 and restart. try {If (pos0 [(INT) f]) {f * = 2.0; Continue ;}} catch (exception e) {}// if F is in]-2.0; -1.0] U [1.0; 2.0 [, return the proper answer. try {return posn [(INT) (F + 1.5)/2)];} catch (exception e) {}// F is outside]-2.0; 2.0 [, divide by 2 and restart. f/= 2.0 ;}} static void check (double F) {system. out. println (F + "->" + positive (f);} public static void main (string ARGs []) {for (double I =-10.0; I <= 10.0; I ++) Check (I); check (-1e24); check (-1e-24); check (1e-24); check (1e24 );}}

The output is:

-10.0 -> false-9.0 -> false-8.0 -> false-7.0 -> false-6.0 -> false-5.0 -> false-4.0 -> false-3.0 -> false-2.0 -> false-1.0 -> false0.0 -> true1.0 -> true2.0 -> true3.0 -> true4.0 -> true5.0 -> true6.0 -> true7.0 -> true8.0 -> true9.0 -> true10.0 -> true-1.0E24 -> false-1.0E-24 -> false1.0E-24 -> true1.0E24 -> true

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.