tags: - examples - calculations - arithmetic - tutorial
Basic Calculations¶
This guide demonstrates fundamental EverSharp calculations including arithmetic operations, comparisons, variables, and simple functions.
Simple Arithmetic¶
Basic Operations¶
// Addition
10 + 5; // Returns: 15
100 + 25.5; // Returns: 125.5
// Subtraction
50 - 20; // Returns: 30
100 - 0.5; // Returns: 99.5
// Multiplication
6 * 7; // Returns: 42
10 * 2.5; // Returns: 25
// Division
100 / 4; // Returns: 25
10 / 3; // Returns: 3.333333...
// Modulo (remainder)
17 % 5; // Returns: 2
100 % 3; // Returns: 1
Order of Operations¶
EverSharp follows standard mathematical precedence (PEMDAS):
// Multiplication before addition
2 +
3 *
4(
// Returns: 14 (not 20)
// Parentheses override precedence
2 + 3
) *
4; // Returns: 20
// Complex expression
10 + 5 * 2 - 8 / 4; // Returns: 18
// Evaluated as: 10 + (5 * 2) - (8 / 4) = 10 + 10 - 2 = 18
Using Variables¶
baseAmount = 1000;
taxRate = 0.08;
// Calculate tax
tax = baseAmount * taxRate; // 80
// Calculate total
total = baseAmount + tax; // 1080
Comparison Operations¶
Numeric Comparisons¶
age = 30;
age > 18; // Returns: true
age >= 30; // Returns: true
age < 65; // Returns: true
age <= 25; // Returns: false
age == 30; // Returns: true
age != 25; // Returns: true
String Comparisons¶
status = "Active";
status == "Active"; // Returns: true
status != "Inactive"; // Returns: true
// String comparison is case-sensitive
status == "active"; // Returns: false
Boolean Logic¶
hasLicense = true;
age = 25;
// AND operator
hasLicense && age >= 18; // Returns: true
// OR operator
age < 18 || age > 65; // Returns: false
// NOT operator
!hasLicense; // Returns: false
Working with Functions¶
Simple Calculation Functions¶
// Function to calculate area of rectangle
calculateArea = function (length, width) {
return length * width;
};
area = calculateArea(10, 5); // Returns: 50
Functions with Multiple Steps¶
// Calculate premium with discount
calculatePremium = function (baseRate, coverageAmount, hasDiscount) {
premium = (baseRate * coverageAmount) / 1000;
if (hasDiscount) {
discount = premium * 0.1;
premium = premium - discount;
}
return premium;
};
premium1 = calculatePremium(2.5, 100000, false); // 250
premium2 = calculatePremium(2.5, 100000, true); // 225 (with 10% discount)
Functions Calling Functions¶
// Calculate gross amount
calculateGross = function (base, multiplier) {
return base * multiplier;
};
// Calculate net amount after deduction
calculateNet = function (base, multiplier, deduction) {
gross = calculateGross(base, multiplier);
return gross - deduction;
};
netAmount = calculateNet(1000, 1.5, 200); // Returns: 1300
// Calculation: (1000 * 1.5) - 200 = 1500 - 200 = 1300
Practical Examples¶
Example 1: Premium Calculator¶
Calculate insurance premium based on age and coverage:
calculateInsurancePremium = function (age, coverageAmount) {
// Base rate per thousand
baseRate = 2.0;
// Age factor
if (age < 30) {
ageFactor = 0.8;
} else if (age < 50) {
ageFactor = 1.0;
} else {
ageFactor = 1.5;
}
// Calculate premium
premium = (coverageAmount / 1000) * baseRate * ageFactor;
return premium;
};
// 25-year-old with $100,000 coverage
premium1 = calculateInsurancePremium(25, 100000); // 160
// 55-year-old with $100,000 coverage
premium2 = calculateInsurancePremium(55, 100000); // 300
Example 2: Discount Calculator¶
Calculate final price with tiered discounts:
calculateFinalPrice = function (originalPrice, quantity) {
// Volume discount tiers
if (quantity >= 100) {
discountRate = 0.2; // 20% off
} else if (quantity >= 50) {
discountRate = 0.15; // 15% off
} else if (quantity >= 10) {
discountRate = 0.1; // 10% off
} else {
discountRate = 0; // No discount
}
// Calculate totals
subtotal = originalPrice * quantity;
discount = subtotal * discountRate;
finalPrice = subtotal - discount;
return finalPrice;
};
// 5 units at $100 each
price1 = calculateFinalPrice(100, 5); // 500 (no discount)
// 25 units at $100 each
price2 = calculateFinalPrice(100, 25); // 2250 (10% off)
// 150 units at $100 each
price3 = calculateFinalPrice(100, 150); // 12000 (20% off)
Example 3: Tax Calculator¶
Calculate tax based on income brackets:
calculateTax = function (income) {
tax = 0;
// Progressive tax brackets
if (income <= 10000) {
// 10% on first $10,000
tax = income * 0.1;
} else if (income <= 40000) {
// 10% on first $10,000 + 12% on remainder
tax = 1000 + (income - 10000) * 0.12;
} else {
// 10% on first $10,000 + 12% on next $30,000 + 22% on remainder
tax = 1000 + 3600 + (income - 40000) * 0.22;
}
return tax;
};
tax1 = calculateTax(8000); // 800
tax2 = calculateTax(25000); // 2800
tax3 = calculateTax(60000); // 9000
Example 4: Compound Interest¶
Calculate investment growth with compound interest:
calculateCompoundInterest = function (principal, annualRate, years) {
// Convert annual rate to decimal
rate = annualRate / 100;
// Calculate final amount
amount = principal;
yearCount = 0;
while (yearCount < years) {
interest = amount * rate;
amount = amount + interest;
yearCount = yearCount + 1;
}
return amount;
};
// $1,000 at 5% for 10 years
finalAmount = calculateCompoundInterest(1000, 5, 10); // 1628.89
// Calculate just the interest earned
principal = 1000;
interest = finalAmount - principal; // 628.89
Example 5: Unit Conversion¶
Convert between different units:
// Temperature conversion
celsiusToFahrenheit = function (celsius) {
return (celsius * 9) / 5 + 32;
};
fahrenheitToCelsius = function (fahrenheit) {
return ((fahrenheit - 32) * 5) / 9;
};
temp1 = celsiusToFahrenheit(100); // 212 (boiling point)
temp2 = fahrenheitToCelsius(32); // 0 (freezing point)
// Distance conversion
milesToKilometers = function (miles) {
return miles * 1.60934;
};
kilometersToMiles = function (kilometers) {
return kilometers / 1.60934;
};
distance1 = milesToKilometers(10); // 16.0934
distance2 = kilometersToMiles(100); // 62.1371
Common Patterns¶
Clamping Values¶
Ensure a value stays within a range:
clamp = function (value, min, max) {
if (value < min) {
return min;
} else if (value > max) {
return max;
} else {
return value;
}
};
result1 = clamp(5, 0, 10); // 5 (within range)
result2 = clamp(-5, 0, 10); // 0 (below min)
result3 = clamp(15, 0, 10); // 10 (above max)
Percentage Calculation¶
// Calculate percentage
calculatePercentage = function (part, whole) {
return (part / whole) * 100;
};
// Calculate value from percentage
calculateFromPercentage = function (percentage, whole) {
return (percentage / 100) * whole;
};
// 25 out of 200
percent = calculatePercentage(25, 200); // 12.5
// What is 15% of 300?
value = calculateFromPercentage(15, 300); // 45
Rounding¶
// Round to nearest integer
rounded = $Round(10.7); // 11
// Round to 2 decimal places
price = 123.456;
displayPrice = $Round(price * 100) / 100; // 123.46
// Round currency
roundCurrency = function (amount) {
return $Round(amount * 100) / 100;
};
total = roundCurrency(19.999); // 20.00
Next Steps¶
- Working with Dates - Date calculations and formatting
- Array Operations - Working with collections of data
- Object Manipulation - Creating and modifying objects
- Business Rules - Complex multi-step calculations
See Also¶
- Operators - Complete operator reference
- Functions - Function declaration and usage
- Mathematics Functions - Built-in math functions