How do I get fraction of x?

68 Views Asked by At
let fraction:CGFloat = 1.0 // This is fraction

let minValue:CGFloat = 60.0 // This is minimum value where slider starts
let maxValue:CGFloat = 240.0 // This is maximum value where slider end

let formula = (((maxValue)-(minValue)) * fraction) + minValue  //min value is 60 here 

 // Above formula translate to = (((240.0)-(60.0)) * 1.0) + 60 
 //to get an answer between 60 to 240 I'm adding "+60"     to the calculated answer and subtracting minValue from max value
//EVERYTHING IS PERFECT TILL HERE

Now I'm confused on how do get reverse the fraction if the value is 74.0

let initialSliderValue:CGFloat = 74.0 
let fractionOf74 = (initialSliderValue/maxValue) // 74.0/240.0 = 0.3083333333

let ans = (maxValue * fractionOf74) + 60 
// How do i get extact fraction to get answer of 74
//PLEASE NOTE I NEED TO ADD +60 to my answer so i get answer between 60 to 250

Thanks for help in advance

2

There are 2 best solutions below

1
On BEST ANSWER

I don't know what language this is, but I believe you can fix this using

let fractionOf74 = (initialSliderValue-minValue)/(maxValue-minValue) // (74.0-60)/(240.0-60) = 0.0777777

Then

let ans = (maxValue - minValue) * fractionOf75 + minvalue //74.0

The key is that you just consider the fraction of the interval [minValue, maxValue], but what you did was considering the fraction of the interval [0, maxValue].

0
On

This is a simple math problem

lets call the fraction you want to be as x (fraction) which ranges from 0 to 1.0

we have the below formulae

let formula = (((maxValue)-(minValue)) * fraction) + minValue //min value is 60 here

When x = 0 formula will result in 60 which is your initial value

when x=1.0 the same formula will result in 240

so, we have

60 = ((240 - 60)*0)+60

and

 240 = ((240 - 60)*1.0)+60

now, we want to know for 74 whats the value of x, our formulae will be

74 = ((240-60)*x)+60

solving the above equation we get

(74 -60) = 180*x

14/180=x

0.07777777777 = x

thanks, Hope this helps!