strictfp
| 日本語 | 厳密浮動小数点 |
| 英語 | strict floating-point |
| ふりがな | すとりくとえふぴー |
| フリガナ | ストリクトエフピー |
浮動小数点を必ずIEEE754で扱うための予約語。
クラスもしくはメソッドを修飾することで、そのクラス内及びメソッド内での浮動小数点の扱いは、必ずIEEE754の仕様に則ったものとなる。
逆に言うと、strictfpで修飾されていない場合、厳密にIEEE754の仕様に則っていない場合がある。これは、浮動小数点の計算は、実際にはJavaではなくCPUが行い、浮動小数点の計算結果がCPUの仕様に依存するためである。
strictfpはこの問題を解決し、常にIEEE754の仕様に則った結果をもたらす。ただし、そこまでこだわるのであれば、動作速度が許せばBigDecimalクラスを使用した方がいいだろう。
クラスもしくはメソッドを修飾することで、そのクラス内及びメソッド内での浮動小数点の扱いは、必ずIEEE754の仕様に則ったものとなる。
逆に言うと、strictfpで修飾されていない場合、厳密にIEEE754の仕様に則っていない場合がある。これは、浮動小数点の計算は、実際にはJavaではなくCPUが行い、浮動小数点の計算結果がCPUの仕様に依存するためである。
strictfpはこの問題を解決し、常にIEEE754の仕様に則った結果をもたらす。ただし、そこまでこだわるのであれば、動作速度が許せばBigDecimalクラスを使用した方がいいだろう。
参考サイト
- (参考サイトはありません)
// Sample.java
public class Sample
{
public static void main( String[] args )
{
Sample sample = new Sample();
sample.strictfpMethod();
}
/**
* strictfpメソッド。
*/
public strictfp void strictfpMethod()
{
double d1 = 2.0;
double d2 = 3.0;
Strictfp.outputDoubleBit( d1 + d2 );
// 0 10000000001 0100000000000000000000000000000000000000000000000000
}
}
/**
* strictfpクラス。
*/
strictfp class Strictfp
{
/**
* double型変数をビット形式で出力します。
*/
public static void outputDoubleBit( double d )
{
// double型変数をビット形式で文字列化します。
String source = Long.toBinaryString( Double.doubleToLongBits( d ) );
// 左0埋めします。
StringBuffer strbuf = new StringBuffer();
for( int iF1 = source.length(); iF1 < 64; ++iF1 )
{
strbuf.append( "0" );
}
strbuf.append( source );
// 符号、仮数部、指数部の間にスペースを入れます。
strbuf.insert( 12, " " );
strbuf.insert( 1, " " );
System.out.println( strbuf.toString() );
}
}
public class Sample
{
public static void main( String[] args )
{
Sample sample = new Sample();
sample.strictfpMethod();
}
/**
* strictfpメソッド。
*/
public strictfp void strictfpMethod()
{
double d1 = 2.0;
double d2 = 3.0;
Strictfp.outputDoubleBit( d1 + d2 );
// 0 10000000001 0100000000000000000000000000000000000000000000000000
}
}
/**
* strictfpクラス。
*/
strictfp class Strictfp
{
/**
* double型変数をビット形式で出力します。
*/
public static void outputDoubleBit( double d )
{
// double型変数をビット形式で文字列化します。
String source = Long.toBinaryString( Double.doubleToLongBits( d ) );
// 左0埋めします。
StringBuffer strbuf = new StringBuffer();
for( int iF1 = source.length(); iF1 < 64; ++iF1 )
{
strbuf.append( "0" );
}
strbuf.append( source );
// 符号、仮数部、指数部の間にスペースを入れます。
strbuf.insert( 12, " " );
strbuf.insert( 1, " " );
System.out.println( strbuf.toString() );
}
}
// Sample.java
public class Sample
{
public static void main( String[] args )
{
Sample sample = new Sample();
sample.strictfpMethod();
}
/**
* strictfpメソッド。
*/
public strictfp void strictfpMethod()
{
double d1 = 2.0;
double d2 = 3.0;
Strictfp.outputDoubleBit( d1 + d2 );
// 0 10000000001 0100000000000000000000000000000000000000000000000000
}
}
/**
* strictfpクラス。
*/
strictfp class Strictfp
{
/**
* double型変数をビット形式で出力します。
*/
public static void outputDoubleBit( double d )
{
// double型変数をビット形式で文字列化します。
String source = Long.toBinaryString( Double.doubleToLongBits( d ) );
// 左0埋めします。
StringBuffer strbuf = new StringBuffer();
for( int iF1 = source.length(); iF1 < 64; ++iF1 )
{
strbuf.append( "0" );
}
strbuf.append( source );
// 符号、仮数部、指数部の間にスペースを入れます。
strbuf.insert( 12, " " );
strbuf.insert( 1, " " );
System.out.println( strbuf.toString() );
}
}




