java - Split a number into 2 parts: above and below a given number N -
i trying model electricity costs. usually, <1000 kwh usage, there per-kwh cost, increases if use >1000kwh month. need single equation gives cost per kwh, given particular model electricity consumption.
given number x , constant n, find , b where:
- a = portion of number below n
- b = portion of number above n
"if" conditions cannot used; single formula needed.
for example:
- for
n=1000
,x=500
, havea=500, b=0
. - for
n=1000
,x=1500
, havea=1000, b=500
.
edit:
to make more clear:
- for x < n, = x mod n, b = 0;
- for x > n, = n, b = x mod n.
edit2:
abs()
operator acceptable, since can implemented using bit operators. abs(x)=((x >>> 30) | 1)) * x
.
how can 2 cases combined single equation?
i think got 1 after support defintion of max version provided you---
max(a,b) = 1/2 (a+b+abs(a-b))
and minimum defintion myside
min(a,b) = 1/2 (a+b-abs(a-b))
.
please improve me wherever wrong criticising :-
b={x-n+abs(x-n)}/2; a={x+n-abs(x-n)}/2; or simply, x-b;
Comments
Post a Comment