Range scaling problem
Solution 1:
If you have a range $[A,B]$ and want to linearly transform it to the range $[C,D]$, which is the simplest option, and I assume the one you are looking for, then the correct function is $$f(x)=C\left(1-\frac{x-A}{B-A}\right)+D\left(\frac{x-A}{B-A}\right).$$ For example, we have that $f(A)=C\cdot 1+ D\cdot0=C$, and $f(B)=C\cdot 0+D\cdot1=D$, so that the minimum of the first range gets sent to the minimum of the second range, and similarly with the maximums. Notice that as $x$ increases from $A$ to $B$, the quantity $\frac{x-A}{B-A}$ changes linearly from 0 to 1. However, there are infinitely many other, non-linear functions sending the range $[A,B]$ to the range $[C,D]$, and which one is best suited to your needs may depend on the meaning of the data you are working with.
So, to linearly scale $[40,100]$ to $[0,100]$, the function works out to $$f(x)=\frac{5}{3}(x-40),$$ and to linearly scale $[8,35]$ to $[0,100]$, the function works out to $$f(x)=\frac{100}{27}(x-8).$$
Solution 2:
Think of it as first scaling the number down to zero (of the domain set), then multiply with the multiplying factor and then scaling it back up from zero (of the range set).
Eg: 0-100 to 0-50. Number to be scaled: 25
Make it to zero of domain : 25-0(OF DOMAIN) = 25
Scale it : 25*(50-0)/(100-0) = 12.5
Make it back up from zero of range : 12.5+0(OF RANGE) = 12.5