Thursday, December 2, 2010

Precision calculation

In one of my developments I came across calculating precision.
Precision is generally like a round-off function i.e. to decide at runtime to what value you want to round-off the number.

e.g. If I'm traveling in a hired car. For each kilometer it will cost me Rs. 10
The car owner will bill me on kilometer basis and if it is 9.37 km, he will bill me for 9.5 km. If I travel 0.75 km he will bill me for 1 km.

In this case the
Base precision value is - 1, means if I travel any distance less than a kilometer, it is going to cost me bill for 1 km.

Incremental precision is - 0.5, means round off the value to next incremental of 0.5 km
For 9.75, it would be 10. For 9.25, it would be 9.5 and so on.

To calculate this I have following code

totalKM = ((distTravelled - basePrecision) > 0 ?
(Math.ceil((distTravelled - basePrecision)
/ (incrPrecision)) * incrPrecision)
+ basePrecision : basePrecision);

1 comment:

Sachin More said...

Good to see you back!!! Keep blogging.

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...