001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.common.math;
017
018import static org.junit.Assert.assertEquals;
019import org.junit.Test;
020
021public class FloatOperationsTest {
022
023    private FloatOperations ops = new FloatOperations();
024
025    @Test
026    public void shouldReturnProperExponenentInScientificNotation() {
027        assertEquals(-3, ops.getExponentInScientificNotation(0.0010f));
028        assertEquals(-3, ops.getExponentInScientificNotation(0.0020f));
029        assertEquals(-3, ops.getExponentInScientificNotation(0.009999f));
030        // assertEquals(-2, ops.getExponentInScientificNotation(0.010000000f) ); // precision is messing this up; actual is
031        // 0.00999
032        assertEquals(-2, ops.getExponentInScientificNotation(0.020000000f));
033        assertEquals(-2, ops.getExponentInScientificNotation(0.09999f));
034        assertEquals(-1, ops.getExponentInScientificNotation(0.10f));
035        assertEquals(-1, ops.getExponentInScientificNotation(0.20f));
036        assertEquals(-1, ops.getExponentInScientificNotation(0.9999f));
037        assertEquals(0, ops.getExponentInScientificNotation(0.0f));
038        assertEquals(0, ops.getExponentInScientificNotation(1.0f));
039        assertEquals(0, ops.getExponentInScientificNotation(2.0f));
040        assertEquals(0, ops.getExponentInScientificNotation(9.999f));
041        assertEquals(1, ops.getExponentInScientificNotation(10.0f));
042        assertEquals(1, ops.getExponentInScientificNotation(20.0f));
043        assertEquals(1, ops.getExponentInScientificNotation(99.999f));
044        assertEquals(2, ops.getExponentInScientificNotation(100.0f));
045        assertEquals(2, ops.getExponentInScientificNotation(200.0f));
046        assertEquals(2, ops.getExponentInScientificNotation(999.999f));
047        assertEquals(3, ops.getExponentInScientificNotation(1000.0f));
048        assertEquals(3, ops.getExponentInScientificNotation(2000.0f));
049        assertEquals(3, ops.getExponentInScientificNotation(9999.999f));
050    }
051
052    @Test
053    public void shouldRoundNumbersGreaterThan10() {
054        assertEquals(101.0f, ops.roundUp(101.2523f, 0), 0.01f);
055        assertEquals(101.0f, ops.roundUp(101.2323f, 0), 0.01f);
056        assertEquals(101.3f, ops.roundUp(101.2523f, 1), 0.01f);
057        assertEquals(101.2f, ops.roundUp(101.2323f, 1), 0.01f);
058        assertEquals(110.0f, ops.roundUp(109.2323f, -1), 1f);
059        assertEquals(100.0f, ops.roundUp(101.2323f, -1), 1f);
060    }
061}