Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
5cf9e3af
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5cf9e3af
编写于
4月 14, 2011
作者:
D
darcy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
Reviewed-by: alanb
上级
79f8abda
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
121 addition
and
28 deletion
+121
-28
src/share/classes/java/lang/Math.java
src/share/classes/java/lang/Math.java
+16
-15
src/share/classes/java/lang/StrictMath.java
src/share/classes/java/lang/StrictMath.java
+7
-13
test/java/lang/Math/RoundTests.java
test/java/lang/Math/RoundTests.java
+98
-0
未找到文件。
src/share/classes/java/lang/Math.java
浏览文件 @
5cf9e3af
/*
* Copyright (c) 1994, 201
0
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 201
1
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
...
...
@@ -627,11 +627,9 @@ public final class Math {
}
/**
* Returns the closest {@code int} to the argument. The
* result is rounded to an integer by adding 1/2, taking the
* floor of the result, and casting the result to type {@code int}.
* In other words, the result is equal to the value of the expression:
* <p>{@code (int)Math.floor(a + 0.5f)}
* Returns the closest {@code int} to the argument, with ties
* rounding up.
*
* <p>
* Special cases:
* <ul><li>If the argument is NaN, the result is 0.
...
...
@@ -649,17 +647,17 @@ public final class Math {
* @see java.lang.Integer#MIN_VALUE
*/
public
static
int
round
(
float
a
)
{
return
(
int
)
floor
(
a
+
0.5f
);
if
(
a
!=
0x1
.
fffffep
-
2
f
)
// greatest float value less than 0.5
return
(
int
)
floor
(
a
+
0.5f
);
else
return
0
;
}
/**
* Returns the closest {@code long} to the argument. The result
* is rounded to an integer by adding 1/2, taking the floor of the
* result, and casting the result to type {@code long}. In other
* words, the result is equal to the value of the expression:
* <p>{@code (long)Math.floor(a + 0.5d)}
* <p>
* Special cases:
* Returns the closest {@code long} to the argument, with ties
* rounding up.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
* <li>If the argument is negative infinity or any value less than or
* equal to the value of {@code Long.MIN_VALUE}, the result is
...
...
@@ -676,7 +674,10 @@ public final class Math {
* @see java.lang.Long#MIN_VALUE
*/
public
static
long
round
(
double
a
)
{
return
(
long
)
floor
(
a
+
0.5d
);
if
(
a
!=
0x1
.
fffffffffffffp
-
2
)
// greatest double value less than 0.5
return
(
long
)
floor
(
a
+
0.5d
);
else
return
0
;
}
private
static
Random
randomNumberGenerator
;
...
...
src/share/classes/java/lang/StrictMath.java
浏览文件 @
5cf9e3af
/*
* Copyright (c) 1999, 201
0
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 201
1
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
...
...
@@ -613,11 +613,8 @@ public final class StrictMath {
public
static
native
double
pow
(
double
a
,
double
b
);
/**
* Returns the closest {@code int} to the argument. The
* result is rounded to an integer by adding 1/2, taking the
* floor of the result, and casting the result to type {@code int}.
* In other words, the result is equal to the value of the expression:
* <p>{@code (int)Math.floor(a + 0.5f)}
* Returns the closest {@code int} to the argument, with ties
* rounding up.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
...
...
@@ -635,15 +632,12 @@ public final class StrictMath {
* @see java.lang.Integer#MIN_VALUE
*/
public
static
int
round
(
float
a
)
{
return
(
int
)
floor
(
a
+
0.5f
);
return
Math
.
round
(
a
);
}
/**
* Returns the closest {@code long} to the argument. The result
* is rounded to an integer by adding 1/2, taking the floor of the
* result, and casting the result to type {@code long}. In other
* words, the result is equal to the value of the expression:
* <p>{@code (long)Math.floor(a + 0.5d)}
* Returns the closest {@code long} to the argument, with ties
* rounding up.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
...
...
@@ -662,7 +656,7 @@ public final class StrictMath {
* @see java.lang.Long#MIN_VALUE
*/
public
static
long
round
(
double
a
)
{
return
(
long
)
floor
(
a
+
0.5d
);
return
Math
.
round
(
a
);
}
private
static
Random
randomNumberGenerator
;
...
...
test/java/lang/Math/RoundTests.java
0 → 100644
浏览文件 @
5cf9e3af
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6430675
* @summary Check for correct implementation of {Math, StrictMath}.round
*/
public
class
RoundTests
{
public
static
void
main
(
String
...
args
)
{
int
failures
=
0
;
failures
+=
testNearFloatHalfCases
();
failures
+=
testNearDoubleHalfCases
();
if
(
failures
>
0
)
{
System
.
err
.
println
(
"Testing {Math, StrictMath}.round incurred "
+
failures
+
" failures."
);
throw
new
RuntimeException
();
}
}
private
static
int
testNearDoubleHalfCases
()
{
int
failures
=
0
;
double
[][]
testCases
=
{
{+
0x1
.
fffffffffffffp
-
2
,
0.0
},
{+
0x1
.
0
p
-
1
,
1.0
},
// +0.5
{+
0x1
.
0000000000001
p
-
1
,
1.0
},
{-
0x1
.
fffffffffffffp
-
2
,
0.0
},
{-
0x1
.
0
p
-
1
,
0.0
},
// -0.5
{-
0x1
.
0000000000001
p
-
1
,
-
1.0
},
};
for
(
double
[]
testCase
:
testCases
)
{
failures
+=
testNearHalfCases
(
testCase
[
0
],
(
long
)
testCase
[
1
]);
}
return
failures
;
}
private
static
int
testNearHalfCases
(
double
input
,
double
expected
)
{
int
failures
=
0
;
failures
+=
Tests
.
test
(
"Math.round"
,
input
,
Math
.
round
(
input
),
expected
);
failures
+=
Tests
.
test
(
"StrictMath.round"
,
input
,
StrictMath
.
round
(
input
),
expected
);
return
failures
;
}
private
static
int
testNearFloatHalfCases
()
{
int
failures
=
0
;
float
[][]
testCases
=
{
{+
0x1
.
fffffep
-
2
f
,
0.0f
},
{+
0x1
.
0
p
-
1
f
,
1.0f
},
// +0.5
{+
0x1
.
000002
p
-
1
f
,
1.0f
},
{-
0x1
.
fffffep
-
2
f
,
0.0f
},
{-
0x1
.
0
p
-
1
f
,
0.0f
},
// -0.5
{-
0x1
.
000002
p
-
1
f
,
-
1.0f
},
};
for
(
float
[]
testCase
:
testCases
)
{
failures
+=
testNearHalfCases
(
testCase
[
0
],
(
int
)
testCase
[
1
]);
}
return
failures
;
}
private
static
int
testNearHalfCases
(
float
input
,
float
expected
)
{
int
failures
=
0
;
failures
+=
Tests
.
test
(
"Math.round"
,
input
,
Math
.
round
(
input
),
expected
);
failures
+=
Tests
.
test
(
"StrictMath.round"
,
input
,
StrictMath
.
round
(
input
),
expected
);
return
failures
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录