Replacing a formula from a string

I have a long formula expressed as a string

formula_1 = “6366.00 * Math.acos(Math.cos(self.latitude) *
Math.cos(latitude) * Math.cos(longitude -self.longitude) +
(Math.sin(self.latitude) * Math.sin(LatitudeRad)) )”

I need to use it in the preparation of some SQL query conditions like :

condition += (c(:villes){ 6366.00 * Math.acos(Math.cos(self.latitude) *
Math.cos(latitude) * Math.cos(longitude -self.longitude) +
(Math.sin(self.latitude) * Math.sin(LatitudeRad)) ) < distance } )

How could I write it only once ‘formula_1’ and reuse it like :
condition += (c(:villes){ formula_1 < distance }) # this is wrong as
formula_1 is a string …

thansk for your suggestions

joss

Josselin wrote:

(Math.sin(self.latitude) * Math.sin(LatitudeRad)) ) < distance } )

How could I write it only once ‘formula_1’ and reuse it like :
condition += (c(:villes){ formula_1 < distance }) # this is wrong as
formula_1 is a string …

The simplest way to solve this problem is to convert your equation into
a
function. Why are you not doing this? Does the equation change
regularly?

If there are as many equations as database records, then of course you
want
to think up a way to interpret the equations as you encounter them. But
if
there is one equation and many sets of variables to apply to it, then
rewrite the text form of your equation as a function. Then read the
database records, convert the values to numerics, and submit them to the
function.

Put simply, it’s called “computer programming”.

From this:

formula_1 = “6366.00 * Math.acos(Math.cos(self.latitude) *
Math.cos(latitude) * Math.cos(longitude -self.longitude) +
(Math.sin(self.latitude) * Math.sin(LatitudeRad)) )”

to this (not tested):

function formula_1(latitude,longitude,latitude_rad)
return 6366.0 * math.acos(@latitude) * Math.cos(latitude) *
Math.cos(longitude - @longitude) + (Math.sin(@latitude) *
Math.sin(latitude_rad)))
end