I need to do this..
<coordinates_east>6'01.4</coordinates_east>
<coordinates_north>45'05.5</coordinates_north>
I need to convert to this google friendly format in Ruby!...please note that these are not the real converted numbers just an example of the format I think I need!
<coordinates_east>45.46998</coordinates_east>
<coordinates_north>6.90764</coordinates_north>
How?
your input coordinates seem to be in wrong notation. it should probably be
<coordinates_north>45°05.5'</coordinates_north>
<coordinates_east>6°01.4'</coordinates_east>
(degrees° arcminutes'), or
<coordinates_north>45°05'5"</coordinates_north>
<coordinates_east>6°01'4"</coordinates_east>
(degrees° arcminutes' arcseconds")
once you figured out the correct input notation, you can use Parsing latitude and longitude with Ruby for converting them to decimal degrees. if your input notation is degrees° arcminutes', you have to modify it slightly. also pay attention to negative coordinates.
if you only want to use it with google maps, you don't actually need to convert it, because google maps understands arcminutes/-seconds notation.
my coordinates were space separated ("deg min sec") and I wrote this little lambda to handle them:
#to_decimal = lambda do |str|
deg, min, sec = str.split(" ").map(&:to_f)
if deg >= 0
output = deg + (min / 60.0) + (sec / 3600.0)
elsif deg < #HARD EARNED KNOWLEDGE HERE
output = deg - (min / 60.0) - (sec / 3600.0)
end
raise "something is wrong" if output.abs > 180
output
end
Related
We had given task as
Read an angle in degrees, and print the tan, cosec, sec, and cot values of that angle in the same order. To use via the rad() function, convert the angle from degrees to radians for the standard library functions.
For which we written code as
x = io.read()
radianVal = math.rad (x)
io.write(string.format("%.1f ", math.tan(radianVal)),"\n")
io.write(string.format("%.1f ", math.cosh(radianVal)),"\n")
io.write(string.format("%.1f ", math.sinh(radianVal)),"\n")
io.write(string.format("%.1f ", math.cos(radianVal)),"\n")
But output is not as expect, not sure where we went wrong
Run as Custom Input
30
Your Output (stdout)
0.6
1.1
0.5
0.9
Expected Output
0.57735026918963
2.0
1.1547005383793
1.7320508075689
Correct code which is working perfectly is
x = io.read()
function cot(radAngle)
return 1 / math.tan(radAngle)
end
function cosec(radAngle)
return 1 / math.sin(radAngle)
end
function sec(radAngle)
return 1 / math.cos(radAngle)
end
local radAngle = math.rad(x)
print(math.tan(radAngle))
print(cosec(math.rad(x)))
print(sec(math.rad(x)))
print(cot(math.rad(x)))
How can you expect to get the correct result if you calculate cosh(x) instead of csc(x)? You're using the hyperbolic cosine to calcuate the cosecant. I think it is obvious why your results are not accepted.
You use sinh(x) to calculate sec(x) and cos(x) to calculate cot(x).
You're confusing mathematical functions. That's not a Lua programming problem.
I suggest you spend a few hours on researching trigonometric functions.
https://en.wikipedia.org/wiki/Trigonometric_functions
Such values are calcuated using the correct formula or function. Not by using any function of the math library that has a similar name.
cosh is deprecated since Lua 5.3 btw.
You can use math.tan to calculate the tangens of your angle.
For secant, cosecant and cotangens you'll have to implement your own functions.
Also note that the number of decimals might cause issue in result validation. Make sure it is ok to round to 1 decimal.
Example:
There is no function to calculate the cotangent of an angle. So we define one.
function cot(radAngle)
return 1 / math.tan(radAngle)
end
local radAngle = math.rad(30)
print("tan(30°):", math.tan(radAngle))
print("cot(30°):", cot(math.rad(30)))
->
tan(30°): 0.57735026918963
cot(30°): 1.7320508075689
I am a little bit new in this language, but I have the basics.
What I want: open a position with stop loss and take profit.
I want to place an order with 100 euro, and I want to set the stop loss to 10 euros, and set the take profit to 5 euros. But as I see the OrderSend method requires lots for placing the order, and levels for stop loss and take profit.
And my problem is: how to calculate these values based on the euro amounts I want to set?
I searched for some lot-pip-etc calculation on the web, but after all what I tried did not work. This is how I wanted to calculate:
double AmountToTradeInEuro = 100;
double AmountToTakeInEuro = 5;
double AmountToMaxLossInEuro = 10;
double Lots = AmountToTradeInEuro / MarketInfo(Symbol(), MODE_TICKVALUE);
double StopLossLevel = AmountToTakeInEuro / MarketInfo(Symbol(), MODE_TICKVALUE);
double TakeProfitLevel = AmountToMaxLossInEuro / MarketInfo(Symbol(), MODE_TICKVALUE);
OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, StopLossLevel, TakeProfitLevel);
Basically I would like to know how to calculate the Lot for 100 euro and how to calculate the Levels for stoploss and takeprofit.
And are the stoploss and takeprofit levels also lots? Or are they different units?
Welcome to MQL4!
First question is about the account currency - if it is USD (or something else not EUR) - you have to make such convertion. Ok, let me describe what to do with the EUR balance.
You can compute lot size based on the stoploss - in such a case you can get tick value using MarketInfo(_Symbol,MODE_TICKVALUE). But you must know the price level where to exit (stoploss), whether it is 1 pips or 100 pips. Let us think that is 100 ticks (which is equal to 10 pips of 5-digit broker). Then, your lot size is
double lot = AmountToMaxLoss / (MarketInfor(_Symbol, MODE_TICKVALUE) * stoploss), then you have to normalize the result:
double lot_step=MarketInfo(_Symbol, MODE_LOTSTEP);
double result = lot_step * NormalizeDouble(lot / lot_step, 0);
then check that result > MarketInfo(_Symbol, MODE_MINLOT).
About takeprofit - it might be a strange approach to wait for your takeprofit target in the currency instead of the price level but if you need - the way is same.
Given any number of the sort 78.689 or 1.12 for instance, what I'm looking for is to programmatically round the number to the nearest tenth place after the decimal.
I'm trying to do this in an environment where there is a math.floor() function that rounds to the lowest whole number, and as far as I can tell from documentation there's nothing like PHP's round() function.
There's simple snippet at: http://lua-users.org/wiki/SimpleRound
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
It will misbehave when numDecimalPlaces is negative, but there's more examples on that page.
You can use coercion to do this...
It work just like printf... You can try to do something like in this snippet.
value = 8.9756354
print(string.format("%2.1f", value))
-- output: 9.0
Considering that this is roblox, it would just be easier to make this a global variable instead of making a single module or creating your own gloo.
_G.round = function(x, factor)
local factor = (factor) and (10 ^ factor) or 0
return math.floor((x + 0.5) * factor) / factor
end
In my case, I was simply trying to make a string representation of this number... however, I imagine this solution could prove useful to others as well.
string.sub(tostring(percent * 100), 1, 4)
so to bring it back to a numerical representation, you could simply call tonumber() on the resulting number.
Hey there guys im working on a project for school and in the project it requires the user the enter decimal coordinates and convert it into structured format, ie:
17.428333° --> 17° 25' 42"
Does anyone possibly know how to do this? Also the program should be able to do it the opposite way; enter structured coordinates 17° 25' 42" and covert to decimal coordinates 17.4283333.
I have got the basic maths used for the converting. To convert from structured coordinates to decimal coordinates you can use the following :
17+25/60+42/3600
and to covert from decimal to structured takes first value 17 as the degrees then to work out the minutes
17.4285-17=0.4285*60=25.71
gets rid of the decimal or rounds the decimal to get the minutes, then to work out the seconds
25.71-25=0.71*60=42.6
either rounds the decimal or gets rid of the decimal for seconds.
Let's assume you start with a coord in a floating point variable x. Calculate degrees, minutes and seconds like this:
Degrees := Trunc(x);
x := (x - Degrees)*60;
Minutes := Trunc(x);
x := (x - Minutes)*60
Seconds := Round(x);
The opposite direction is simpler. The expression you need is:
Degrees + Minutes/60 + Seconds/3600
I need to convert a latitude in ddmm.mmmmm (minutes in 4 decimal places) to ddmm.mmmmmm (minutes in 5 decimal places) format. Is there any good formula to convert this ?
I got the answer
We need to follow these steps for this conversion
1. Convert value in ddmm.mmmm format to dd.ddddddd by using the following formula
dd.ddddddd = dd + ( mm.mmmm / 60 )
convert back
ddmm.mmmmm = concat(dd, (.dddddd * 60))
Example:
To convert 3323.8733 from ddmm.mmmm format
convert to degrees (dd.dddd) format
33 + (23.8733 / 60 ) = 33.397888333333334
convert back to ddmm.mmmmm format
multiply decimal part by 60 i.e 0.397888333333334 * 60 => 23.87330000000004
append with degree
3323.87330000000004
As we need ddmm.mmmmm we can round of 5 decimal places i.e 3323.87330
Sans other information I would recommend following mkk's advice.
If you want to convert "ddmm.mmmmm" (4 decimal places) to "ddmm.mmmmm" (5 decimal places), you should probably just add a zero to the end.
Other methods may appear to give a more satisfactory result by placing a non-zero value in the fifth decimal place. But they cannot add more information than was present in the original number. There is, however, the potential to lose information through loss of significance in mathematical calculations.