I am looking on a way to convert decimals to degrees in C. For instance, the asin() function in C returns a decimal number but I need that number to be in degrees ° minutes ' seconds ".
e.g. 1.5 would be 1°30'0"
The asin function returns radians. There are 2 π radians in a circle.
There are 360 degrees in a circle, 60 minutes in a degree, and 60 seconds in a minute. So there are 360*60*60 seconds in a circle.
double radians = asin(opposite / hypotenuse);
int totalSeconds = (int)round(radians * 360 * 60 * 60 / (2 * M_PI));
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int degrees = totalSeconds / (60 * 60);
Not sure how to do this with one command like >dms on the ti84, but you can use logic.
The whole units of degrees will remain the same (i.e. in 121.135°
longitude, start with 121°).
Multiply the decimal by 60 (i.e. .135 * 60 = 8.1).
The whole number becomes the minutes (8').
Take the remaining decimal and multiply by 60. (i.e. .1 * 60 = 6).
The resulting number becomes the seconds (6"). Seconds can remain as a
decimal.
Take your three sets of numbers and put them together, using
the symbols for degrees (°), minutes (‘), and seconds (") (i.e.
121°8'6" longitude)
Source:
http://geography.about.com/library/howto/htdegrees.htm
A little bit of searching and i found this in c#:
Converting from Decimal degrees to Degrees Minutes Seconds tenths.
double decimal_degrees;
// set decimal_degrees value here
double minutes = (decimal_degrees - Math.Floor(decimal_degrees)) * 60.0;
double seconds = (minutes - Math.Floor(minutes)) * 60.0;
double tenths = (seconds - Math.Floor(seconds)) * 10.0;
// get rid of fractional part
minutes = Math.Floor(minutes);
seconds = Math.Floor(seconds);
tenths = Math.Floor(tenths);
But as he said, it will need to be convered from radians to degrees first.
Related
The delta_s function calculates the difference of time between 2 dates in the dates with seconds. Then the average median and max values for the differences is calculated. I am trying to convert the times in seconds for average median and max values but it does not work. i want to convert it in the form of x days x hours x minutes x seconds.
Code:
import numpy as np
dates= np.array(['2017-09-15 07:11:00' ,'2017-09-15 11:25:30', '2017-09-15 12:11:10', '2021-04-07 22:43:12', '2021-04-08 00:49:18'],
dtype="datetime64[ns]")
delta_s = np.diff(dates) // 1e9 # nanoseconds to seconds
delta_s = delta_s.astype(np.float64)
delta_avg = np.average(delta_s)
delta_median= np.median(delta_s)
delta_max = np.max(delta_s)
delta_max_index= np.argmax(delta_s)
The line delta_s = np.diff(dates) // 1e9 does not actually convert nanoseconds to seconds. It simply divides 1e9 to the timedelta object but the time unit is preserved timedelta64[ns].
>>> np.diff(dates)
array([ 15270000000000, 2740000000000, 112357922000000000,
7566000000000], dtype='timedelta64[ns]')
>>> np.diff(dates) // 1e9
array([ 15270, 2740, 112357922, 7566],
dtype='timedelta64[ns]')
This may mess up with any calculations you're doing.
Use
delta_s = np.array([np.timedelta64(td, 's') for td in np.diff(dates) ])
Currently there are no inbuilt functions to format timedelta to strings. However you can use something of this sort.
# Function to convert seconds to Human readable Timedelta string
def seconds_to_tdstring(total_seconds):
days, remainder = divmod(total_seconds, 60 * 60 * 24)
hours, remainder = divmod(remainder, 60 * 60)
minutes, seconds = divmod(remainder, 60)
return '{:02} Days {:02} Hours {:02} Minutes {:02} Seconds'.format(int(days), int(hours), int(minutes), int(seconds))
print(seconds_to_tdstring(delta_avg))
Output:
325 Days 04 Hours 24 Minutes 34 Seconds
I have modified the answer from a similar question .
How can I round a number to interval of 60 ?
For instance if I have number 61, It should round of to 60 and if number is 150, it should round of to 120 and in case of 59 it should round off to zero.
To truncate values multiply by 60 and divide by 60. See demo:
SELECT 61/60*60 --result: 60
SELECT 59/60*60 --result: 0
Number must be int (bigint, smallint, tinyint). If it's not, use CAST/CONVERT.
See also Division (Transact SQL):
If an integer dividend is divided by an integer divisor, the result is
an integer that has any fractional part of the result truncated.
ugly but it works:
Integer Division Then Multiply
SELECT (125 / 60 ) * 60
Think this is what you're after
DECLARE #TestVal INT = 59
SELECT #TestVal - (#TestVal % 60)
SET #TestVal = 61
SELECT #TestVal - (#TestVal % 60)
SET #TestVal = 150
SELECT #TestVal - (#TestVal % 60)
Alternative is to use the modulus operator to get the remainder of a division by 60 then subtract it:
% (Modulus) (Transact-SQL)
Returns the remainder of one number divided by another.
For example:
declare #valueToTest int = 150
select #valueToTest - (#valueToTest % 60) as result
-- result: 120
So this gets the remainder when you divide the #valueToTest by 60 and subtracts it from the original #valueToTest.
I'd prefer FLOOR over relying on Integer division, it's what FLOOR is for. example
DECLARE #YourVal DECIMAL(10,4) = 220.1234;
SELECT #YourVal / 60 * 60, FLOOR(#YourVal / 60) * 60;
How numerators and denominators of days, hours and minutes are calculated in this code, why modulus is calculated in numerator?
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
Let me explain it line by line:
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
In the above line, you are getting the milliseconds for the date Sep 5, 2018 15:37:25 from Jan 1, 1970 (which is the reference date being used by getTime()
var now = new Date().getTime();
var distance = countDownDate - now;
The above two lines are simple. now gets the current time in milliseconds and distance is the difference between the two times (also in milliseconds)
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
The total number of seconds in a day is 60 * 60 * 24 and if we want to get the milliseconds, we need to multiply it by 1000 so the number 1000 * 60 * 60 * 24 is the total number of milliseconds in a day. Dividing the difference (distance) by this number and discarding the values after the decimal, we get the number of days.
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
The above line is a little tricker as there are two operations. The first operation (%) is used to basically discard the part of the difference representing days (% returns the remainder of the division so the days portion of the difference is taken out.
In the next step (division), 1000 * 60 * 60 is the total number of milliseconds in an hour. So dividing the remainder of the difference by this number will give us the number of hours (and like before we discard the numbers after decimal)
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
This is similar to how hours are calculated. The first operation (%) takes out the hours portion from difference and the division (1000*60) returns the minutes (as 1000 * 60 is the number of milliseconds in a minute)
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
Here the first operation (%) takes out the minutes part and the second operation (division) returns the number of seconds.
Note: You might have noticed that in every operation the original distance is used but the code still works fine. Let me give you an example (I am using difference instead of distance as this name makes more sense).
difference = 93234543
days = Math.floor(89234543 / (1000 * 60 * 60 * 24))
=> days = 1
hours = Math.floor((89234543 % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
(result of modulus operation is 6834543, and division is )
=> hours = 1
This is a very important operation to understand:
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
distance(difference) / (1000 * 60 * 60) returns 25 (hours). As you can see we have already got 1 day and 1 hour (25 hours) so distance % (1000 * 60 * 60) wipes out all of these 25 hours and then the division calculates the minutes and so on.
A call is charged 30 cents per minute.
The cost for line rental is RM60.00. The tax for the overall bill including the line rental) is 15%.
Calculate the amount, that needs to be paid by the user given the number of
minutes that the user uses his/her mobile phone.
How can I transform this formula into C code?
rate = (minute*0.30)+15/100 *60
The formula is wrong. You first need to multiply the number of minutes by 30 cents, then add the 60 for the rental, and only then apply the tax by multiplying by 1.15:
int minutes = // inputted from user...
double total = ((0.3 * minutes) + 60) * 1.15;
I think is just that:
float minute;
float rate;
printf("How many minutes?");
scanf("%f", &minute);
rate = (minute * 0.3 + 60) + 0.15 * (minute * 0.3 + 60);
I often hear the term "linear interpolation" in context with animations in WPF. What exactly does "linear interpolation" mean? Could you give me an example where to use "linear interpolation"?
Linear means lines (straight ones).
Interpolation is the act of finding a point within two other points. Contrast this with extrapolation, which is finding a point beyond the ends of a line.
So linear interpolation is the use of a straight line to find a point between two others.
For example:
*(5,10)
/
/
/
/
*(0,0)
You can use the two endpoints with linear interpolation to get the points along the line:
(1,2)
(2,4)
(3,6)
(4,8)
and linear extrapolation to get (for example):
(1000,2000)
(-1e27,-2e27)
In animation, let's say you have a bouncing ball that travels from the (x,y) position of (60,22) to (198,12) in 10 seconds.
With an animation rate of 10 frames per second, you can calculate it's position at any time with:
x0 = 60, y0 = 22
x1 = 198, y1 = 12
frames = 100
for t = 0 to frames:
x = (x1 - x0) * (t / frames) + x0
y = (y1 - y0) * (t / frames) + y0
Those two formulae at the bottom are examples of linear interpolation. At 50% (where t == 50):
x = (198 - 60) * (50 / 100) + 60
= 138 * 0.5 + 60
= 69 + 60
= 129
y = (12 - 22) * (50 / 100) + 22
= -10 * 0.5 + 22
= -5 + 22
= 17
and (129,17) is the midpoint between the starting and ending positions.
E.g. when you want a storyboard to move an element from one position to another using a fixed speed, then you'd use linear interpolation between the start and end positions.