I would like to confirm my calculation for data transfer:
Assume, I have 1 PB of data and a network bandwidth of 1 Gbps, how long will it take to transfer the data.
I have the following calculation:
1 PB = 1,000,000 GB (decimal)
Therefore, it will take 1,000,000 / (60 * 60 * 24) = 11.57 days to transfer the data.
Is this correct?
I think it will be (1,000,000 * 8) / (60 * 60 * 24) = (11.57 * 8) .
Since 1B = 8 bit
var days = Math.floor(distance / (1000 * 60 * 60 * 24));enter code here
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));enter code here
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));enter code here
var seconds = Math.floor((distance % (1000 * 60)) / 1000);enter code here
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 wrote a simple function to fill three variables with the current year, month, and day.
However, for some reason it is not working correctly, and I can't seem to find the problem.
void getDate(int *year, int *month, int *date)
{
int epochTime,
monthLength,
functionYear,
functionMonth,
functionDate;
functionYear = 1970;
functionMonth = 1;
functionDate = 1;
epochTime = time(NULL);
while (epochTime > 1 * 365 * 24 * 60 * 60)
{
epochTime -= 1 * 365 * 24 * 60 * 60;
functionYear++;
}
monthLength = findMonthLength(functionYear, functionMonth, false);
while (epochTime > 1 * monthLength * 24 * 60 * 60)
{
printf("%d\n", epochTime);
epochTime -= 1 * monthLength * 24 * 60 * 60;
functionMonth++;
monthLength = findMonthLength(functionYear, functionMonth, false);
printf("functionMonth = %d\n", functionMonth);
}
while (epochTime > 1 * 24 * 60 * 60)
{
printf("%d\n", epochTime);
epochTime -= 1 * 24 * 60 * 60;
functionDate++;
printf("functionDate = %d\n", functionDate);
}
*year = functionYear;
*month = functionMonth;
*date = functionDate;
}
findMonthLength() returns an integer value which the length of the month it is sent. 1 = January, etc. It uses the year to test if it is a leap year.
It is currently April 3, 2013; however, my function finds April 15, and I can't seem to find where my problem is.
EDIT:
I got it. My first problem was that while I remembered to check for leap years when finding the months, I forgot about that when finding each year, which put me several days off.
My second problem was that I didn't convert to the local time zone from UTC
One problem could in this section:
while (epochTime > 1 * 365 * 24 * 60 * 60)
{
epochTime -= 1 * 365 * 24 * 60 * 60;
functionYear++;
}
Each iteration of this loop, a time in seconds corresponding to one normal year is subtracted. This does not account for leap years, where you need to subtract a time corresponding to 366 days.
For that section, you may want:
int yearLength = findYearLength(functionYear + 1);
while (epochTime > 1 * yearLength * 24 * 60 * 60)
{
epochTime -= 1 * yearLength * 24 * 60 * 60;
functionYear++;
yearLength = findYearLength(functionYear + 1);
}
with findYearLength(int year) being a function that returns the length in days of a given year.
One minor issue is that leap seconds are not accounted for. As only 35 of these have been added, that can be safely ignored in a calculation for a given day.
Why not use gmtime() or localtime() and be done with it? They return a structure with everything you need in it.
I made the complete solution based on yours, in Python. I hope it will help someone, someday. Cheers!
Use: getDate(unixtime)
def leapYear(year):
#returns True if the year is a leap year, return False if it isn't
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
def findMonthLength(year, month):
#returns an integer value with the length of the month it is sent.
#1 = January, etc. It uses the year to test if it is a leap year.
months1 = [0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
months2 = [0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (leapYear(year)==True):
return months2[month]
else:
return months1[month]
def findYearLength(year):
#returns an integer value with the length of the year it is sent.
#It uses the year to test if it is a leap year.
if leapYear(year)==True:
return 366
else:
return 365
def getDate(epoch):
SECONDS_PER_YEAR = 0
SECONDS_PER_MONTH = 0
SECONDS_PER_DAY = 24 * 60 * 60
SECONDS_PER_HOUR = 60*60
SECONDS_PER_MIN = 60
epochTime = epoch
monthLength = 0
yearLength = 0
year = 1970
month = 1
day = 1
hour = 0
minu = 0
seg = 0
#Years
yearLength = findYearLength(year)
SECONDS_PER_YEAR = yearLength * SECONDS_PER_DAY
while (epochTime >= SECONDS_PER_YEAR):
epochTime -= SECONDS_PER_YEAR
year += 1
yearLength = findYearLength(year)
SECONDS_PER_YEAR = yearLength * SECONDS_PER_DAY
#Months
monthLength = findMonthLength(year, month)
SECONDS_PER_MONTH = monthLength * SECONDS_PER_DAY
while (epochTime >= SECONDS_PER_MONTH):
epochTime -= SECONDS_PER_MONTH;
month += 1
monthLength = findMonthLength(year, month)
SECONDS_PER_MONTH = monthLength * SECONDS_PER_DAY
#Days
while (epochTime >= SECONDS_PER_DAY):
epochTime -= SECONDS_PER_DAY;
day += 1
#Hours
while (epochTime >= SECONDS_PER_HOUR):
epochTime -= SECONDS_PER_HOUR;
hour += 1
#Minutes
while (epochTime >= SECONDS_PER_MIN):
epochTime -= SECONDS_PER_MIN;
minu += 1
#Seconds
seg = epochTime
print ("%d-%d-%d %d:%d:%d") % (year, month, day, hour, minu, seg)
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.