Simple rounding with Coldfusion - coldfusion

I have a scenario where I need to round to the next 5kgs.
if the total weight is less than 5kgs, I need to round it to the next 5kgs.
if the item is just over 5kgs, for instance, 5.01 it needs to round up to 10kgs.
My math skills aren't the greatest, so Im hoping someone might be able to show me how to calculate it so it jumps to the next 5 kgs automatically
<cfset totalWeight = 5.01>
<cfset breakPoints = 5.00>
<cfset bagCost = 18.00>
<cfset totalWeight = ceiling(totalWeight)>
<cfif totalWeight LTE 5.00 >
<cfset totalBags = 1 >
<cfelse>
<cfset totalBags = totalWeight / breakpoints >
</cfif>
<cfset totalCost = totalBags * bagCost>
<cfoutput>
#totalweight#<br/>
#totalBags#
<hr/>
#totalCost#<br/>
<hr/>
</cfoutput>

To do something like this, the math logic is to divide the original value by 5, round it, and then multiply by 5. This guarantees you the next multiple of 5.
<CFSET RoundedValue = ceiling(totalWeight / 5) * 5>
Since you always want to round up, we use the CEILING function. If merely rounding to the nearest 5 (up or down), we would use the ROUND function instead.

Related

Python 2 - Division times 10 returns 0 everytime

When the program divides two user inputted numbers, then times that, the program returns 0 every time
correct = input("User, Input the amount of correct answers. :")
points_possible = input("User, Input the amount of points possible. :")
score = correct / points_possible
grade = score * 10
print grade
The expected output
if (1/2) * 10 = 5, but will output 0
If you are using Python 2.7 version, the input that would be taken from the console would always be in the form of strings. So, you'll need to convert it to integers.
#code in Python 3.5
correct = int(input("User, Input the amount of correct answers. :"))
points_possible = int(input("User, Input the amount of points possible. :"))
score = correct / points_possible
grade = score * 10
print(grade)
The reason why you're just getting 0 in Python 2 is that if integers are given then you'll get integer division only. If you want float division, you need to make sure there is a decimal point somewhere so that Python knows it doesn't have to truncate the value to an int.
#code in Python 2.7
correct = float(raw_input("User, Input the amount of correct answers. :"))
points_possible = float(raw_input("User, Input the amount of points possible. :"))
score = correct / points_possible
grade = score * 10.0
print grade
it's because python needs you to make it understand that you divide by a float: you can either add .0 at the end of the diviser or type 10/float(2)

Unexpected behaviour of int() [duplicate]

This question already has answers here:
Why does this subtraction not equal zero?
(7 answers)
Closed 6 years ago.
I'm a bit confused about following behaviour of the int()-function in ColdFusion 10 (10,0,20,282462):
<cfset dummy = 100 - (5859 / (6510 / 100)) />
<cfoutput>
dummy = #dummy#<br><br> <!--- 10 --->
int(10) = #int(10)#<br> <!--- 10 --->
int(dummy) = #int(dummy)# <!--- 9 --->
</cfoutput>
Can anybody explain me why int(dummy) returns 9 instead of 10?
int(dummy) returns 9 instead of 10 because it is essentially floor() in other languages, and your answer may become 9 because for performance, by default, they are treated as double.
Have you heard of PrecisionEvaluate() https://cfdocs.org/precisionevaluate
dummy = PrecisionEvaluate(100 - (5859 / (6510 / 100)));
writeOutput(dummy);
you'll get 10 as expected

strange behavior with Coldfusion loops?

Today I have written small program in ColdFusion to find the square root of a number. I found a strange issue. Not sure am I doing any silly mistake ?
<cfscript>
num = 16;
n = 0.0001;
i = 0;
for (i=0;i<num;i=i+n)
{
if((i*i)>num){
i = i - n;
break;
}
}
writedump(i);
</cfscript>
The output is 3.999 instead of 4 when num = 16. Where as when I set it to 36 its output is 6.
Not sure why I am getting 3.999 where as it should be 4.Any suggestion?
I altered your code to output the conditions you are seeing like so:
<cfscript>
num = 16.00;
n = 0.0001;
i = 0;
for (i=0;i<num;i=i+n)
{
writeoutput(i & " i * i = " & (i*i));
writeoutput('<br>');
if((i*i)>num){
i = i - n;
break;
}
}
writedump(i);
writeoutput('<br>');
</cfscript>
This outputs all the numbers and the multiples - a long list. The last 3 lines look like this:
3.9999 i * i = 15.99920001
4 i * i = 16
3.9999
That seems like expected behavior to me. Inside your break code you are reducing the value of i with this line:
i = i - n;
Is that not what you want?
Why do you want to do all the heavy lifting when we can use power of java as below:
<cfset testNumber = 20.50 />
<cfset mathObj = createObject( "java", "java.lang.Math" ) />
<cfset sqrtNumber = mathObj.sqrt(testNumber) />
<cfdump var="#sqrtNumber#"><cfabort>
Output:
4 -> 2
6 -> 2.44948974278
36- > 6
20.50 - > 4.52769256907
As you can see it works on all values including decimals. I hope this helps.
Note: Before passing values to the mathObj.sqrt, you should first check for negative values. You can not have sqrt of negative numbers.
Edit:
As Leigh pointed out there is already a function in CF, you can use that as follows:
<cfset testNumber = 36 />
<cfset sqrtNumber = sqr(testNumber) />
You will get output same as Java version code, only difference is that when you pass negative value in java code, you will get a gibberish value. With CF, you will get an error like this The 1 parameter of the Sqr function, which is now -122.0, must be a non-negative real number.
There is a problem in the if condition. When you use num=36 in that case at 6.001 the if condition gets evaluated and you get 6. But in the case of num=16 the if condition gets evaluated at 4.00 and you get 3.999.
But you may ask why at 4.00 it gets evaluated due to floating point numbers and floating point arithmetic.
Edit:
You can use Newton method to find square root of any +ve number.By this method you can achieve better performance over for loop that you had used.

Why is python skipping a line?

I'm pretty new to Python (just started teaching myself a week ago), so my debugging skills are weak right now. I tried to make a program that would ask a user-submitted number of randomly-generated multiplication questions, with factors between 0 and 12, like a multiplication table test.
import math
import random
#establish a number of questions
questions = int(input("\n How many questions do you want? "))
#introduce score
score = 1
for question in range(questions):
x = random.randrange(0,13)
y = random.randrange(0,13)
#make the numbers strings, so they can be printed with strings
abc = str(x)
cba = str(y)
print("What is " + abc + "*" + cba +"?")
z = int(input("Answer here: "))
print z
a = x*y
#make the answer a string, so it can be printed if you get one wrong
answer = str(a)
if z > a or z < a:
print ("wrong, the answer is " + answer)
print("\n")
#this is the line that's being skipped
score = score - 1/questions
else:
print "Correct!"
print ("\n")
finalscore = score*100
finalestscore = str(finalscore)
print (finalestscore + "%")
The idea was that every time the user gets a question wrong, score (set to 1) goes down by 1/question,so when multiplied by 100 it gives a percentage of questions wrong. However, no matter the number of questions or the number gotten wrong, score remains 1, so finalestscore remains 100. Line 26 used to be:
if math.abs(z)-math.abs(a) != 0:
but 2.7.3 apparently doesn't acknowledge that math has an abs function.
Such a simple accumulator pattern doesn't seem like it would be an issue, even for an older version of Python. Help?
Try score = score - 1.0/questions
The problem is that you're doing integer division, which truncates to the nearest integer, so 1/questions will always give 0.
The problem is that you are using integers for all of your calculations. In particular, when you calculate 1/questions, it truncates (rounds down) to an integer because both values in the calculation are integers.
To avoid this, you could instead use 1.0/questions to make the calculations use floating point numbers instead (and not truncate)

ColdFusion - Converting weight in pounds to pounds and ounces

I am writing a little conversion piece in ColdFusion CFSCRIPT.
I need to convert a weight in pounds to pounds and ounces.
So, 3.1565 needs to become 3 pounds and 3 ounces. 1.512 will become 1 pound and 9 ounces (round up the ounces).
0.25 will become 0 pounds and 4 ounces.
My thought is to take the total weight in pounds and multiply it by sixteen, which will give me the total ounces. Then I'll need to extract the even pounds by dividing by sixteen and the remainder will be the ounces. I really don't know how to do this accurately and with efficient code.
<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
// REMAINDER IS OUNCES - ROUND UP
}
</cfscript>
Something like this (not extensively tested).
EDIT: If the input can be negative, use the abs() value for calculations
<cfset theInput = 0.25>
<!--- round down to get total pounds --->
<cfset lbs = int(theInput)>
<!--- extract remainder. multiply by 16 and round up --->
<cfset ounces = ceiling((theInput - lbs) * 16)>
<cfoutput>#lbs# pounds #ounces# ounces</cfoutput>
Integer division and Modulus should give you the values you need.
<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
weightInPounds = MyOunces \ 16;
// REMAINDER IS OUNCES - ROUND UP
remainderOunces = ceiling(MyOunces MOD 16);
</cfscript>
This should do it:
<cffunction name="PoundConverter" returntype="string">
<cfargument name="Pounds" type="numeric" required="true" hint="" />
<cfset var TotalPounds = Fix(Arguments.Pounds) />
<cfset var TotalOunces = Ceiling((Arguments.Pounds - TotalPounds) * 16) />
<cfreturn TotalPounds & " pounds and " & TotalOunces & " ounces" />
</cffunction>
<cfoutput>
#PoundConverter(3.1565)#<br />
#PoundConverter(1.512)#
</cfoutput>
You pretty much have what you need. To extract the number of pounds, divide by 16. The remainder (the "mod") is the ounces.
<cfscript>
function poundsandounces( initvalue ) {
var rawvalue = val( initvalue ) * 16;
var lbs = int( rawvalue / 16 );
var oz = ceiling( rawvalue % 16 );
return "#lbs# pounds #oz# ounces";
}
</cfscript>
<cfoutput>#poundsandounces( 0.25 )#</cfoutput>