View | Details | Raw Unified | Return to bug 53327
Collapse All | Expand All

(-)src/java/org/apache/xmlgraphics/util/DoubleFormatUtil.java (-10 / +13 lines)
Lines 164-171 Link Here
164
                } else if (digits == 0) {
164
                } else if (digits == 0) {
165
                    long decP = Long.parseLong(intS);
165
                    long decP = Long.parseLong(intS);
166
                    format(target, scale, 0L, decP);
166
                    format(target, scale, 0L, decP);
167
                } else if (decLength < digits) {
168
                    long decP = Long.parseLong(intS) * tenPow(decLength + 1) + Long.parseLong(decS) * 10;
169
                    format(target, exposant + decLength, 0L, decP);
167
                } else {
170
                } else {
168
                    long subDecP = decLength <= digits ? Long.parseLong(decS) * tenPow(digits - decLength) : Long.parseLong(decS.substring(0, digits));
171
                    long subDecP = Long.parseLong(decS.substring(0, digits));
169
                    long decP = Long.parseLong(intS) * tenPow(digits) + subDecP;
172
                    long decP = Long.parseLong(intS) * tenPow(digits) + subDecP;
170
                    format(target, scale, 0L, decP);
173
                    format(target, scale, 0L, decP);
171
                }
174
                }
Lines 182-188 Link Here
182
     * @return true if the source value will be rounded to zero
185
     * @return true if the source value will be rounded to zero
183
     */
186
     */
184
    private static boolean isRoundedToZero(double source, int decimals, int precision) {
187
    private static boolean isRoundedToZero(double source, int decimals, int precision) {
185
        return Math.abs(source) < 5.0 / tenPow(Math.max(decimals, precision) + 1);
188
        return source == 0.0 || Math.abs(source) < 5.0 / tenPowDouble(Math.max(decimals, precision) + 1);
186
    }
189
    }
187
190
188
    /**
191
    /**
Lines 281-292 Link Here
281
        int scale = (source >= 1.0) ? decimals : precision;
284
        int scale = (source >= 1.0) ? decimals : precision;
282
285
283
        long intPart = (long) Math.floor(source);
286
        long intPart = (long) Math.floor(source);
284
        long tenScale = tenPow(scale);
287
        double tenScale = tenPowDouble(scale);
285
        double fracUnroundedPart = (source - intPart) * tenScale;
288
        double fracUnroundedPart = (source - intPart) * tenScale;
286
        long fracPart = Math.round(fracUnroundedPart);
289
        long fracPart = Math.round(fracUnroundedPart);
287
        if (fracPart >= tenScale) {
290
        if (fracPart >= tenScale) {
288
            intPart++;
291
            intPart++;
289
            fracPart -= tenScale;
292
            fracPart = Math.round(fracPart - tenScale);
290
        }
293
        }
291
        if (fracPart != 0L) {
294
        if (fracPart != 0L) {
292
            // Remove trailing zeroes
295
            // Remove trailing zeroes
Lines 308-314 Link Here
308
                // append fractional part
311
                // append fractional part
309
                target.append('.');
312
                target.append('.');
310
                // insert leading zeroes
313
                // insert leading zeroes
311
                while (scale > 0 && fracPart < tenPow(--scale)) {
314
                while (scale > 0 && fracPart < tenPowDouble(--scale)) {
312
                    target.append('0');
315
                    target.append('0');
313
                }
316
                }
314
                target.append(fracPart);
317
                target.append(fracPart);
Lines 356-366 Link Here
356
    private static boolean tooCloseToRound(double source, int scale) {
359
    private static boolean tooCloseToRound(double source, int scale) {
357
        source = Math.abs(source);
360
        source = Math.abs(source);
358
        long intPart = (long) Math.floor(source);
361
        long intPart = (long) Math.floor(source);
359
        double fracPart = (source - intPart) * tenPow(scale);
362
        double fracPart = (source - intPart) * tenPowDouble(scale);
360
        double distanceToRound = Math.abs(fracPart - Math.floor(fracPart) - 0.5);
363
        double range = .001;
361
        // For huge scale, we have to reduce the range.
364
        double distanceToRound1 = Math.abs(fracPart - Math.floor(fracPart));
362
        double range = scale > 12 ? .1 : .001;
365
        double distanceToRound2 = Math.abs(fracPart - Math.floor(fracPart) - 0.5);
363
        return distanceToRound <= range;
366
        return distanceToRound1 <= range || distanceToRound2 <= range;
364
        // .001 range: Totally arbitrary range,
367
        // .001 range: Totally arbitrary range,
365
        // I never had a failure in 10e7 random tests with this value
368
        // I never had a failure in 10e7 random tests with this value
366
        // May be JVM dependent or architecture dependent
369
        // May be JVM dependent or architecture dependent

Return to bug 53327