当前位置: 代码迷 >> J2SE >> 关于java强制类型转换的溢出有关问题 - 求解
  详细解决方案

关于java强制类型转换的溢出有关问题 - 求解

热度:245   发布时间:2016-04-24 02:19:08.0
关于java强制类型转换的溢出问题 --- 求解
package day02all.day02.copy;
/** 类型转换的演示 */
public class CastDemo {
  public static void main(String[] args){
  long l = 65536 + 1024 + 512 + 8;
  // 000000 ....... 00000001 00000110 00001000 
  byte b = (byte)l;//
  System.out.println(b);//8
  //强制类型转换会造成数据的损失.精度缺失
  double pi = 3.1415926535897932384;
  System.out.println(pi);
  float f = (float)pi;
  System.out.println(f);
   
  double d = Math.sqrt(2);//开平方
  float f1 = (float)d;
  System.out.println(d);
  System.out.println(f1);
  //price 价格
  }
}



8
3.141592653589793
3.1415927//(1)
1.4142135623730951
1.4142135(2)

问题:为什么(1)和(2)一个产生进位一个不产生进位?
详细讲解下关于计算机如何处理溢出的问题。





------解决方案--------------------
这个问题还一直没注意过,只知道double转float会发生精度丢失,具体会如何丢失还真没仔细研究过
借LZ的问题调查了一下,把2进制打出来试试看了看

Java code
double pi = 3.1415926535897932384;//System.out.println(pi);float f = (float)pi;//System.out.println(f);String sd = Long.toBinaryString(Double.doubleToLongBits(pi));System.out.printf("%64s\n", sd);System.out.printf("%64s\n", sd.substring(12));String sf = Integer.toBinaryString(Float.floatToIntBits(f));System.out.printf("%32s\n", sf);System.out.printf("%32s\n", sf.substring(9));System.out.printf("%s\n", sd.substring(12));System.out.printf("%s\n", sf.substring(9));System.out.println();        double d = Math.sqrt(2);float f1 = (float)d;//System.out.println(d);//System.out.println(f1);sd = Long.toBinaryString(Double.doubleToLongBits(d));System.out.printf("%64s\n", sd);System.out.printf("%64s\n", sd.substring(12));sf = Integer.toBinaryString(Float.floatToIntBits(f1));System.out.printf("%32s\n", sf);System.out.printf("%32s\n", sf.substring(9));System.out.printf("%s\n", sd.substring(12));System.out.printf("%s\n", sf.substring(9));打印结果 100000000001001001000011111101101010100010001000010110100011000             001001000011111101101010100010001000010110100011000 1000000010010010000111111011011          00100100001111110110110010010000111111011010101000100010000101101000110000010010000111111011011  11111111110110101000001001111001100110011111110011101111001101              10101000001001111001100110011111110011101111001101  111111101101010000010011110011           10101000001001111001110101000001001111001100110011111110011101111001101101010000010011110011
------解决方案--------------------
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.Timer;

public class Tetris extends JFrame {
public Tetris() {
Tetrisblok a = new Tetrisblok();
addKeyListener(a);
add(a);
}

public static void main(String[] args) {
Tetris frame = new Tetris();
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
JMenu game = new JMenu("游戏");
JMenuItem newgame = game.add("新游戏");
JMenuItem pause = game.add("暂停");
JMenuItem goon = game.add("继续");
JMenuItem exit = game.add("退出");
JMenu help = new JMenu("帮助");
JMenuItem about = help.add("关于");
menu.add(game);
menu.add(help);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  相关解决方案