Skip to content

Commit

Permalink
Fix rounding bug by using float literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Radovan Zvoncek committed Feb 2, 2015
1 parent c3e5102 commit d1093ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.annotations.VisibleForTesting;
import com.spotify.reaper.core.RepairRun;
import com.spotify.reaper.core.RepairUnit;

Expand Down Expand Up @@ -87,11 +88,16 @@ public RepairRunStatus(RepairRun repairRun, RepairUnit repairUnit) {
this.startTime = repairRun.getStartTime();
this.endTime = repairRun.getEndTime();
this.pauseTime = repairRun.getPauseTime();
this.intensity = Math.round(repairRun.getIntensity() * 10000) / 10000;
this.intensity = roundIntensity(repairRun.getIntensity());
this.segmentCount = repairUnit.getSegmentCount();
this.repairParallelism = repairUnit.getRepairParallelism().name().toLowerCase();
}

@VisibleForTesting
protected static double roundIntensity(double intensity) {
return Math.round(intensity * 10000f) / 10000f;
}

@JsonProperty("creation_time")
public String getCreationTimeISO8601() {
if (creationTime == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.spotify.reaper.resources.view;

import org.junit.Test;

import static org.junit.Assert.*;

public class RepairRunStatusTest {

@Test
public void testRoundIntensity() throws Exception {
assertEquals(0.0f, RepairRunStatus.roundIntensity(0.0f), 0.00000f);
assertEquals(0.1f, RepairRunStatus.roundIntensity(0.1f), 0.00001f);
assertEquals(0.2f, RepairRunStatus.roundIntensity(0.2f), 0.00001f);
assertEquals(0.3f, RepairRunStatus.roundIntensity(0.3f), 0.00001f);
assertEquals(0.4f, RepairRunStatus.roundIntensity(0.4f), 0.00001f);
assertEquals(0.5f, RepairRunStatus.roundIntensity(0.5f), 0.00001f);
assertEquals(0.6f, RepairRunStatus.roundIntensity(0.6f), 0.00001f);
assertEquals(0.7f, RepairRunStatus.roundIntensity(0.7f), 0.00001f);
assertEquals(0.8f, RepairRunStatus.roundIntensity(0.8f), 0.00001f);
assertEquals(0.9f, RepairRunStatus.roundIntensity(0.9f), 0.00001f);
assertEquals(1.0f, RepairRunStatus.roundIntensity(1.0f), 0.00001f);
}
}

0 comments on commit d1093ab

Please sign in to comment.