Round up a decimal - python-3.x

I have a problem and i need your help!
Here is the code:
kg_lemons = float(input())
kg_sugar = float(input())
water = float(input())
total_lemon_juice = kg_lemons * 980 #in mililiters need to multiply by 1000
total_lemonade = total_lemon_juice + 5 * 1000 + (0.3 * kg_sugar)
cups_made = total_lemonade / 150
money_made = cups_made * 1.20
print(f'All cups sold: {cups_made:.2f}')
print(f'Money earned: {money_made:.2f}')
At then end, after I print it, it must shown the numbers:
All cups sold: 66
Money earned: 79.20
But I got:
All cups sold: 66.01
Money earned: 79.21
So I need to round it up to the second decimal (the lowest number). Should I use math.floor and, if so, how?

Related

Signed float to hexadecimal number

How to convert float to a specific format in hexadecimal:
1 bit for sign, 15 bit for the integer value, and the rest 16 bits for values after the decimal point.
Example output should be ffff587a for -0.6543861, fff31a35 for -12.897631, 006bde10 for 107.8674316, 003bd030 for 59.8132324
I have written a program that can do the unsigned conversion, I am stuck at the signed part. Could anyone guide me on how I can achieve this in a very compact way?
def convert(num):
binary2 = ""
Int = int(num)
fract = num - Int
binary = '{:16b}'.format(Int & 0b1111111111111111)
for i in range (16):
fract *= 2
fract_bit = int(fract)
if fract_bit == 1:
fract -= fract_bit
binary2 += '1'
else:
binary2 += '0'
return int(binary + binary2, 2)
value = 107.867431640625
x = convert(value)
hex(x)
output: 0x6bde10
This is simply the Q16.16 fixed-point format. To convert a floating-point number to this format, simply multiply it by 216 (in Python, 1<<16 or 65536) and convert the product to an integer:
y = int(x * (1<<16))
To show its 32-bit two’s complement representation, add 232 if it is negative and then convert it to hexadecimal:
y = hex(y + (1<<32 if y < 0 else 0))
For example, the following prints “0xfff31a35”:
#!/usr/bin/python
x=-12.897631
y = int(x * (1<<16))
y = hex(y + (1<<32 if y < 0 else 0))
print(y)
This conversion truncates. If you want rounding, you can add .5 inside the int or you can add additional code for other types of rounding. You may also want to add code to handle overflows.

Calculate probability of an event not by exclusion

I have some doubt with these kind of problems, example:
"If we asked 20,000 in a stadium to toss a coin 10 times, what it's the probability of at least one person getting 10 heads?"
I took this example from Practical Statistics for Data Scientist.
So, the probability of at least one person getting 10 heads it's calculated using: 1 - P(of nobody in the stadium getting 10 heads).
So we kind of doing an exclude procedure here, first I get the probability of the contrary event I am trying to measure, not the ACTUAL experiment I want to measure: at least one people getting 10 heads.
Why do we do it this way?
How can I calculate the probability of at least someone getting 10 heads but without passing through the probability of no one getting 10 heads?
As #Robert Dodier mentioned in the comments, the reason is that the calculations are simpler. I will use a stadium of 20 people instead of 20000 as an example:
Method 1:
Probability of not getting 10 heads for one individual
= 1 - probability of getting 10 heads
= 1 - 10!/(10!0!)*0.5^10*(1-0.5)^0
= 0.9990234375
Probability of at least one person in the stadium getting 10 heads
= 1 - P(of nobody in the stadium getting 10 heads)
= 1 - 0.9990234375**20 (because all coin tosses are independent)
= 0.019351109194852834
Method 2:
Probability of getting 10 heads for one individual
= 10!/(10!0!)*0.5^10*(1-0.5)^0
= 0.0009765625
Probability of exactly 1, 2, 3, etc. persons in the stadium getting 10 heads:
p1 = 20!/(1!19!)*0.0009765625^1*(1-0.0009765625)^(20-1) = 0.019172021325613825
p2 = 20!/(2!18!)*0.0009765625^2*(1-0.0009765625)^(20-2) = 0.00017803929872270904
p3 = 20!/(3!17!)*0.0009765625^3*(1-0.0009765625)^(20-3) = 1.0442187608370032e-06
p4 = 20!/(4!16!)*0.0009765625^4*(1-0.0009765625)^(20-4) = 4.338152232216289e-09
p5 = 20!/(5!15!)*0.0009765625^5*(1-0.0009765625)^(20-5) = 1.3569977656981548e-11
p6 = 20!/(6!14!)*0.0009765625^6*(1-0.0009765625)^(20-6) = 3.316221323798032e-14
p7 = 20!/(7!13!)*0.0009765625^7*(1-0.0009765625)^(20-7) = 6.483326146232712e-17
p8 = 20!/(8!12!)*0.0009765625^8*(1-0.0009765625)^(20-8) = 1.029853859983202e-19
p9 = 20!/(9!11!)*0.0009765625^9*(1-0.0009765625)^(20-9) = 1.342266353839299e-22
p10 = 20!/(10!10!)*0.0009765625^10*(1-0.0009765625)^(20-10) = 1.443297154665913e-25
p11 = 20!/(11!9!)*0.0009765625^11*(1-0.0009765625)^(20-11) = 1.2825887804726853e-28
p12 = 20!/(12!8!)*0.0009765625^12*(1-0.0009765625)^(20-12) = 9.403143551852531e-32
p13 = 20!/(13!7!)*0.0009765625^13*(1-0.0009765625)^(20-13) = 5.656451493707817e-35
p14 = 20!/(14!6!)*0.0009765625^14*(1-0.0009765625)^(20-14) = 2.7646390487330485e-38
p15 = 20!/(15!5!)*0.0009765625^15*(1-0.0009765625)^(20-15) = 1.0809927854283668e-41
p16 = 20!/(16!4!)*0.0009765625^16*(1-0.0009765625)^(20-16) = 3.3021529369146104e-45
p17 = 20!/(17!3!)*0.0009765625^17*(1-0.0009765625)^(20-17) = 7.59508466888531e-49
p18 = 20!/(18!2!)*0.0009765625^18*(1-0.0009765625)^(20-18) = 1.2373875315877011e-52
p19 = 20!/(19!1!)*0.0009765625^19*(1-0.0009765625)^(20-19) = 1.2732289258503896e-56
p20 = 20!/(20!0!)*0.0009765625^20*(1-0.0009765625)^(20-20) = 6.223015277861142e-61
Probability of at least one person in the stadium getting 10 heads
= p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 +
p11 + p12 + p13 + p14 + p15 + p16 + p17 + p18 + p19 + p20
= 0.01935110919485281
So the result is the same (the tiny difference is due to floating point precision), but as you can see the first calculation is slightly simpler for 20 people, never mind for 20000 ;)

How do I fix this code to get the proper output

This is the code:
amount_borrowed = float(input("Amount borrowed: $"))
interest_rate = float(input("Interest rate: "))
loan_length = int(input("Length of loan (months): "))
i = interest_rate/100
monthly_payment1 = (i / 12) * amount_borrowed #this is the first part of the payment formula
monthly_payment2 = monthly_payment1 / 1 - (1 + i / 12)**-loan_length
#second part of monthly payment formula
print("The monthly payment is ${:.2f}" .format(monthly_payment2))
I should get this output:
Amount borrowed: $100.00
Interest rate: 10.0
Length of loan (months): 12
The monthly payment is $8.79.
I keep on getting this:
Amount borrowed: $100
Interest rate: 10
Length of loan (months): 12
The monthly payment is $-0.07
Don't make much sense divide by 1. I think that you did forget a pair of parentheses in the denominator of the division. So, the following line
monthly_payment2 = monthly_payment1 / 1 - (1 + i / 12)**-loan_length
should be
monthly_payment2 = monthly_payment1 / (1 - (1 + i / 12)**-loan_length)
I tested here and get the expect value of $8.79

Azure Usage Scaled Rates Pricing

I try to calculate the price for a meterID that has scaled rates . I use this
as guide for the algorithm ( public static double computeRatedUsagePerMeter(Dictionary rates, double usage) )
https://github.com/PartnerCenterSamples/Commerce-API-DotNet/blob/master/Usage.cs
Comparing with the price from azure pricing calculator if i ask the price for quantity X in calculator it is equal to the price that i calculate from the above method but for quantity X - 1.
So i am confused if the method provided from Microsoft is complete or not , or maybe just a hint for the right direction.
private static decimal computeRatedUsagePerMeter(Dictionary<decimal, decimal> rates, decimal usage)
{
decimal total = Decimal.Zero;
if (rates.Count == 0)
return Decimal.Zero;
else if (rates.Count == 1)
return (usage * rates.Values.FirstOrDefault());
var remainingUsage = usage;
while (rates.Count > 0)
{
decimal LastKey = rates.Keys.Last();
if (remainingUsage > LastKey)
{
decimal LastKeyValue = Decimal.Zero;
if (rates.TryGetValue(LastKey, out LastKeyValue))
{
total = total + ((remainingUsage - LastKey + 1) * LastKeyValue); // remainingUsage - LastKey +1 because tiered pricing is exclusive
remainingUsage = LastKey - 1;
}
rates.Remove(LastKey);
}
else if (remainingUsage <= LastKey)
{
rates.Remove(LastKey);
}
}
return total;
}
{
"MeterId": "d23a5753-ff85-4ddf-af28-8cc5cf2d3882",
"MeterName": "Standard IO - Page Blob/Disk (GB)",
"MeterCategory": "Storage",
"MeterSubCategory": "Locally Redundant",
"Unit": "GB",
"MeterTags": [],
"MeterRegion": "",
"MeterRates": {
"0": 0.042165,
"1024": 0.0421650,
"51200": 0.0421650,
"512000": 0.0421650,
"1024000": 0.0379485,
"5120000": 0.0312021
},
"EffectiveDate": "2014-02-01T00:00:00Z",
"IncludedQuantity": 0.0
}
According to the method provided by the link above the price for quantity 1 = 0.084330 while azure pricing calculator gives 0.04
( the prices are in EUR )
And another example : les say 100 quantity.
method: 4.258665 EUR
Azure Calculator = 4.22 EUR
method for 99 quantity = 4.216500 which rounded is 4.22 EUR.
Also cannot check the prices < 1.00 lets say 0.5 quantity ( in this case its measured in GB so 0,5 GB is perfectly reasonable quantity ) cause pricing calculator doesn't allow decimal .
According to the method provided by the link above the price for
quantity 1 = 0.084330 while azure pricing calculator gives 0.04 ( the
prices are in EUR )
Looking at the code above, I believe there's an issue in the code itself. Essentially you're trying to find the price for 1 GB of storage which would fall under 0 - 1023 range or in other words the value of LastKey is 0. So when the following code executes:
total = total + ((remainingUsage - LastKey + 1) * LastKeyValue);
it gives you a total of 0.084330 (0 + (1 - 0 + 1) * 0.042165).
Also cannot check the prices < 1.00 lets say 0.5 quantity ( in this
case its measured in GB so 0,5 GB is perfectly reasonable quantity )
cause pricing calculator doesn't allow decimal.
I am sure someone from Microsoft would provide a proper answer as to why they designed the calculator the way it is designed.

frequency modulation (FM) code snippet

I've written the following code for frequency modulation of an audio signal. The audio itself is 1 sec long, sampled at 8000 Hz. I want to apply FM to this audio signal by using a sine wave with a frequency of 50 Hz (expressed as a fraction of the sampling frequency). The modulating signal has a modulation index of 0.25 so as to create only one pair of sidebands.
for (i = 0; i < 7999; i++) {
phi_delta = 8000 - 8000 * (1 + 0.25 * sin(2* pi * mf * i));
f_phi_accum += phi_delta; //this can have a negative value
/*keep only the integer part that'll be used as an index into the input array*/
i_phi_accum = f_phi_accum;
/*keep only the fractional part that'll be used to interpolate between samples*/
r_phi_accum = f_phi_accum - i_phi_accum;
//If I'm getting negative values should I convert them to positive
//r_phi_accum = fabs(f_phi_accum - i_phi_accum);
i_phi_accum = abs(i_phi_accum);
/*since i_phi_accum often exceeds 7999 I have to add this if statement so as to prevent out of bounds errors */
if (i_phi_accum < 7999)
output[i] = ((input[i_phi_accum] + input[i_phi_accum + 1])/2) * r_phi_accum;
}
Your calculation of phi_delta is off by a factor of 8000 and an offset - it should be 1 +/- a small value, i.e.
phi_delta = 1.0 + 0.25 * sin(2.0 * pi * mf * i));
which will result in phi_delta having a range of 0.75 to 1.25.

Resources