V
V
Vladislav2016-05-21 00:00:13
Programming
Vladislav, 2016-05-21 00:00:13

How to get a value by percentage in a range of two numbers?

For example:
A value of 10% in the range between 0.0 and 20.0 would be 2.0
A value of 50% in the range between -5.0 and 5.0 would be 0
The difficulty is that this is a vector calculation, and - on - does not give here +
The range can be, for example, ( -5.0, -10.0), (5.0, 10.0), (-5.0, 10.0), in general, go both up and down.
This task arose in the development of the game. Example:

(float StartValue, float EndValue, float Percent) {
  const float OnePercent = (EndValue - StartValue) / 100.0f;
  return StartValue + OnePercent * Percent;
}

But this calculation only works if both numbers are positive and if StartValue is always lower than EndValue.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Moseychuk, 2016-05-21
@bullet28

Read about interpolation. Your example is quite correct both with negative numbers and with the reverse order. Think better.
UPD : moreover, this formula is valid for vectors of arbitrary dimension, and for angles, and for many other quantities.

#
#algooptimize #bottize, 2016-05-21
@user004

Формула универсальная для любых чисел, шаг в сторону endvalue от startvalue
Чем не устраивает формула , покажите на примере конкретных вычислений.

F
fatlander, 2016-05-22
@fatlander

EndValue-((EndValue-StartValue)*percent/100)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question