Skip to content

Liters per 100 km to mpg conversion issue #177

@eulogi

Description

@eulogi

The conversion from liters per 100 km to miles per gallon is not working as expected. You can execute this JUnit test to observe this issue:

package com.test;

import javax.measure.Unit;
import javax.measure.quantity.Volume;

import org.junit.Assert;
import org.junit.Test;

import systems.uom.quantity.Consumption;
import systems.uom.unicode.CLDR;

/**
 * Test unit conversions.
 */
public class UnitConversionTest {

    @Test
    @SuppressWarnings("unchecked")
    public void testLiterPer100KilometersToMilesPerGallon() {
        // Just comment that the only value that seems to be working fine is 1 liter per 100 km.
        double literPer100Kilometers = 10;
        Unit<Consumption<Volume>> MILE_PER_GALLON = CLDR.MILE_PER_GALLON.asType(Consumption.class);
        double milesPerGallonActual = CLDR.LITER_PER_100KILOMETERS.getConverterTo(MILE_PER_GALLON).convert(literPer100Kilometers);
        double milesPerGallonExpected = (100 * 3.785411784) / (1.609344 * literPer100Kilometers);
        Assert.assertEquals(milesPerGallonExpected, milesPerGallonActual, 0.001);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testLiterPer100KilometersToMilesPerGallonImperial() {
        double literPer100Kilometers = 10;
        Unit<Consumption<Volume>> MILE_PER_GALLON_IMPERIAL = CLDR.MILE.divide(CLDR.GALLON_IMPERIAL).asType(Consumption.class);
        double milesPerGallonImperialActual = CLDR.LITER_PER_100KILOMETERS.getConverterTo(MILE_PER_GALLON_IMPERIAL).convert(literPer100Kilometers);
        double milesPerGallonImperialExpected = 282.480936332 / literPer100Kilometers;
        Assert.assertEquals(milesPerGallonImperialExpected, milesPerGallonImperialActual, 0.001);
    }
}

Related to unitsofmeasurement/uom-demos#97

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions