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, have a=500, b=0.
  • for n=1000 , x=1500, have a=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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -