You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/10/26 10:19:58 UTC

[commons-numbers] 02/03: Use String.format when doubles are used in messages.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit d3475e98dd807367f23bd2669dc538b9dd3eb2b5
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Oct 26 11:02:46 2021 +0100

    Use String.format when doubles are used in messages.
    
    MessageFormat.format by default will represent numbers with too low a
    precision. E.g. nextUp(1.5) is 1.5 not 1.5000000000000002.
---
 .../java/org/apache/commons/numbers/gamma/GammaException.java     | 8 +++-----
 .../java/org/apache/commons/numbers/gamma/InvGamma1pm1Test.java   | 7 +++++--
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java
index 9c0eae1..da98b96 100644
--- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java
@@ -16,26 +16,24 @@
  */
 package org.apache.commons.numbers.gamma;
 
-import java.text.MessageFormat;
-
 /**
  * Package private exception class with constants for frequently used messages.
  */
 class GammaException extends IllegalArgumentException {
     /** Error message for "out of range" condition. */
-    static final String OUT_OF_RANGE = "Number {0} is out of range [{1}, {2}]";
+    static final String OUT_OF_RANGE = "Number %s is out of range [%s, %s]";
 
     /** Serializable version identifier. */
     private static final long serialVersionUID = 20170505L;
 
     /**
      * Create an exception where the message is constructed by applying
-     * the {@code format()} method from {@code java.text.MessageFormat}.
+     * the {@code format()} method from {@code java.lang.String}.
      *
      * @param message  the exception message with replaceable parameters
      * @param formatArguments the arguments for formatting the message
      */
     GammaException(String message, Object... formatArguments) {
-        super(MessageFormat.format(message, formatArguments));
+        super(String.format(message, formatArguments));
     }
 }
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InvGamma1pm1Test.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InvGamma1pm1Test.java
index 868202c..7618b4b 100644
--- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InvGamma1pm1Test.java
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InvGamma1pm1Test.java
@@ -86,8 +86,11 @@ class InvGamma1pm1Test {
 
     @Test
     void testInvGamma1pm1Precondition2() {
-        Assertions.assertThrows(GammaException.class,
-            () -> InvGamma1pm1.value(1.51)
+        final double x = Math.nextUp(1.5);
+        GammaException ex = Assertions.assertThrows(GammaException.class,
+            () -> InvGamma1pm1.value(x)
         );
+        Assertions.assertTrue(ex.getMessage().contains(Double.toString(x)),
+            () -> ex.getMessage() + " missing " + Double.toString(x));
     }
 }