Java中运行表达式return 1.0/0.0会发生什么?return 1/0会发生什么?

本文详细解析了Java中1.0/0.0与1/0的运算结果及异常处理,阐述了浮点数与整数除法的区别,并介绍了如何判断NaN与Infinity。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Java中运行表达式:1.0 / 0.0,会有返回么?会不会抛出异常或者是编译器error? 
那1/0呢? 

① 浮点数除法,1.0/0.0不会抛出异常,值为Infinity。

This is another tricky question from Double class. Though Java 
developer knows about the double primitive type and Double class, 
while doing floating point arithmetic, they don’t pay enough attention 
to Double.INFINITY, NaN, and -0.0 and other rules that govern the 
arithmetic calculations involving them. The simple answer to this 
question is that it will not throw ArithmeticExcpetion and return 
Double.INFINITY.

② 整数除以0时会抛出ArithmeticException。

Exception in thread "main" java.lang.ArithmeticException: / by zero

补充:如何判断一个x是否为NaN or Infinity? 
不能使用 return x == NaN,因为无论如何,返回均为false,即使x为NaN; 
必须使用Doubl类的静态方法isNaN(); 
示例:

public class Main1 {

    public static void main(String[] args) {
            double x = Double.NaN;
            System.out.println(x == Double.NaN);
            System.out.println(Double.isNaN(x));
    }
}


输出:

false
true
1
2
对Infinity同理:

public class Main1 {

    public static void main(String[] args) {
            double x = 1.0 / 0.0;
            System.out.println(x == Double.NEGATIVE_INFINITY);
            System.out.println(x == Double.POSITIVE_INFINITY);
            System.out.println(Double.isInfinite(x));
    }
}


输出:

false
true
true
1
2
3
可以看出 1.0/0.0为POSTIVE_INFINITY,说明java里面0.0是+0.0。如果1.0/0.0由于默写原因导致符号反转了,上述代码将等到相反结果。为了避免符号带来的麻烦,提高程序鲁棒性,建议使用Double.isInfinite()

#include <reg52.h> #include <intrins.h> #include <stdio.h> // ?????? - ??JD51??? sbit LED = P1^0; // LED???P1.0 sbit KEY_BRIGHT = P3^2; // ???????P3.2 sbit KEY_SWITCH = P3^3; // ???????P3.3 sbit BUZZER = P2^0; // ??????P2.0 sbit DS18B20 = P3^7; // DS18B20???P3.7 sbit LCD_RS = P2^1; // LCD RS???P2.1 sbit LCD_RW = P2^2; // LCD RW???P2.2 sbit LCD_EN = P2^3; // LCD EN???P2.3 #define LCD_DATA P0 // LCD??????P0? // ???? bit led_state = 1; // LED??(1=?,0=?) bit key_flag = 0; // ???? unsigned char brightness = 2; // ????(0-4) unsigned int pwm_count = 0; // PWM??? float temperature = 0.0; // ??? bit alarm_state = 0; // ???? bit beep_active = 0; // ??????? unsigned int beep_timer = 0; // ?????? unsigned char beep_mode = 0; // ????? // ????? #define TEMP_IDLE 0 #define TEMP_WAIT_CONV 1 #define TEMP_READ 2 unsigned char temp_state = TEMP_IDLE; unsigned int temp_wait = 0; // ???? void Timer0_Init(void); void Beep(unsigned int duration, unsigned char mode); void LCD_Init(void); void LCD_WriteCmd(unsigned char cmd); void LCD_WriteData(unsigned char dat); void LCD_ShowString(unsigned char x, unsigned char y, char *str); void Update_Display(void); bit DS18B20_Reset(void); void DS18B20_WriteByte(unsigned char dat); unsigned char DS18B20_ReadByte(void); void Temp_Alarm_Check(void); void delay_us(unsigned int us); void delay_ms(unsigned int ms); void Process_Temperature(void); void Key_Scan(void); // ???? - ??11.0592MHz?? void delay_us(unsigned int us) { while(us--) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } // ???? - ??11.0592MHz?? void delay_ms(unsigned int ms) { unsigned int i, j; for(i = 0; i < ms; i++) for(j = 0; j < 114; j++); // ?????? } // ???0??? - ??SST89E58RD void Timer0_Init() { TMOD |= 0x01; // ??1,16???? TH0 = 0xFC; // ??1ms?? (11.0592MHz) TL0 = 0x66; ET0 = 1; // ?????0?? TR0 = 1; // ?????0 EA = 1; // ?????? } // ???0?????? void Timer0_ISR() interrupt 1 { TH0 = 0xFC; // ???? TL0 = 0x66; // PWM??LED?? pwm_count = (pwm_count + 1) & 0x03; LED = led_state && (pwm_count < brightness); // ????? if(beep_active) { if(beep_timer > 0) { beep_timer--; BUZZER = ~BUZZER; // ???? } else if(beep_mode == 1) { // ???? beep_timer = 500; // ????? } else { // ???? beep_active = 0; BUZZER = 0; // ????? } } } // DS18B20???? bit DS18B20_Reset(void) { bit res; DS18B20 = 0; delay_us(480); // 480us????? DS18B20 = 1; delay_us(60); // 60us?? res = DS18B20; // ?????? delay_us(420); // ?????? return !res; // ???????? } // DS18B20??? void DS18B20_WriteByte(unsigned char dat) { unsigned char i; for(i = 0; i < 8; i++) { DS18B20 = 0; // ????? _nop_(); DS18B20 = dat & 0x01; // ???? delay_us(60); // ??60us DS18B20 = 1; // ???? dat >>= 1; // ????? } } // DS18B20??? unsigned char DS18B20_ReadByte(void) { unsigned char i, dat = 0; for(i = 0; i < 8; i++) { DS18B20 = 0; // ????? _nop_(); _nop_(); // 2us?? DS18B20 = 1; // ???? _nop_(); _nop_(); // 2us?? if(DS18B20) dat |= (1 << i); // ????? delay_us(60); // ??60us } return dat; } // ??????? void Process_Temperature(void) { static unsigned char temp_buf[2]; switch(temp_state) { case TEMP_IDLE: if(DS18B20_Reset()) { DS18B20_WriteByte(0xCC); // ??ROM DS18B20_WriteByte(0x44); // ?????? temp_state = TEMP_WAIT_CONV; temp_wait = 750; // 750ms???? } break; case TEMP_WAIT_CONV: if(temp_wait-- == 0) { temp_state = TEMP_READ; } break; case TEMP_READ: if(DS18B20_Reset()) { DS18B20_WriteByte(0xCC); // ??ROM DS18B20_WriteByte(0xBE); // ????? temp_buf[0] = DS18B20_ReadByte(); // ????? temp_buf[1] = DS18B20_ReadByte(); // ????? // ????? temperature = ((temp_buf[1] << 8) | temp_buf[0]) * 0.0625; } temp_state = TEMP_IDLE; // ?????? Temp_Alarm_Check(); // ?????? break; } } // ?????? void Temp_Alarm_Check(void) { if(temperature > 30.0) { if(!alarm_state) { alarm_state = 1; // ?????? Beep(500, 1); // ?????(????) } } else if(alarm_state) { alarm_state = 0; // ?????? beep_active = 0; // ????? BUZZER = 0; } } // ??????? void Beep(unsigned int duration, unsigned char mode) { beep_active = 1; // ????? beep_mode = mode; // ????(0=??,1=??) beep_timer = duration * 2; // ????? } // ?????? void Key_Scan(void) { static unsigned char key1_cnt = 0, key2_cnt = 0; // ?????? if(!KEY_BRIGHT) { if(key1_cnt < 20) key1_cnt++; if(key1_cnt == 10) { // ???? brightness = (brightness + 1) % 5; // ????(0-4) key_flag = 1; // ???????? } } else { key1_cnt = 0; } // ?????? if(!KEY_SWITCH) { if(key2_cnt < 20) key2_cnt++; if(key2_cnt == 10) { // ???? led_state = !led_state; // ??LED?? key_flag = 1; // ???????? } } else { key2_cnt = 0; } } // LCD??? void LCD_Init(void) { LCD_WriteCmd(0x38); // 8?????,2??? LCD_WriteCmd(0x0C); // ???,??? LCD_WriteCmd(0x06); // ????,???? LCD_WriteCmd(0x01); // ?? delay_ms(2); // ?????? } // LCD??? void LCD_WriteCmd(unsigned char cmd) { LCD_RS = 0; // ???? LCD_RW = 0; // ??? LCD_DATA = cmd; LCD_EN = 1; // ???? delay_us(10); LCD_EN = 0; delay_ms(2); // ?????? } // LCD??? void LCD_WriteData(unsigned char dat) { LCD_RS = 1; // ???? LCD_RW = 0; // ??? LCD_DATA = dat; LCD_EN = 1; // ???? delay_us(10); LCD_EN = 0; delay_ms(1); // ?????? } // LCD????? void LCD_ShowString(unsigned char x, unsigned char y, char *str) { // ?????? if(y == 0) LCD_WriteCmd(0x80 + x); else LCD_WriteCmd(0xC0 + x); // ?????? while(*str) { LCD_WriteData(*str++); } } // ?????? void Update_Display(void) { char temp_str[17]; // ???:LED????? sprintf(temp_str, "LED:%-3s Brt:%d", led_state?"ON":"OFF", brightness); LCD_ShowString(0, 0, temp_str); // ???:??????? sprintf(temp_str, "Temp:%-6.2fC", temperature); LCD_ShowString(0, 1, temp_str); // ?????? LCD_ShowString(13, 1, alarm_state?"ALARM":" "); } // ??? void main(void) { unsigned int timer = 0; // ??????? // ????? Timer0_Init(); // ???0??? LCD_Init(); // LCD??? // ?????? LCD_ShowString(0, 0, "JD51 LED Control"); LCD_ShowString(0, 1, "SST89E58RD Ready"); delay_ms(1000); LCD_WriteCmd(0x01); // ?? // ??? while(1) { Key_Scan(); // ???? Process_Temperature(); // ???? // ???????? if(key_flag) { key_flag = 0; Update_Display(); } // ??????(??1?) if(++timer >= 1000) { timer = 0; Update_Display(); } delay_ms(1); // ??CPU?? } } 所有的元件都在开发板上只需调用
最新发布
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值