calculation

Set Limit For Value, iPhone

Hello all, I have the following code: float valueCalculated = (val1 / val2) * 100; What I want to be able to do is to cap the maximum value of valueCalculated to 100. I believe I could do this using some sort of if statement, but this would mean many more lines of code. Edit// This is not the case, see the answers below. Thanks, ...

Averaging angles... Again

I want to calculate the average of a set of angles, which represents source bearing (0 to 360 deg) - (similar to wind-direction) I know it has been discussed before (several times). The accepted answer was Compute unit vectors from the angles and take the angle of their average. However this answer defines the average in a non intuitiv...

jQuery IE easing error

Hi, I'm experiencing a strange error in IE on this site: http://www.davenuttall.co.uk The fund-raising barometer on the left-hand side animates upwards to the correct position just fine in all other browsers, but throws an 'invalid argument' error in IE. I think it's caused by the easing method - easeOutElastic - from the easing plugi...

PHP Calculate Balanced Arrays

Ok, I'm not sure if the title is that effective but its the best I could come up with. Basically here is the scenario. I have 11 categories. In each category, I have items, but one category might have 1 item, 1 might have 20. Now i want to separate the 11 categories into 5 stacks or columns. I want each of the stacks to contain an eq...

How to perform an accurate currency calculation in an iPhone app

I've written an iPhone App that calculates a total cost by adding/subtracting units of predetermined cost. This was working fine until I realised that the float values I was using caused the total to be inaccurate so I started to look into using NSDecimalNumber. I'm now rather confused and have an 'out of scope' error. Relevant sections ...

Confused over some date/time calculations

So, I am trying to calculate the time between two dates that fits certain criteria (here: work / non-work) and I'm confused about the results as I can't find out why it's wrong. But first, some code; **Input Date A:** 2009-01-01 2:00 pm **Input Date B:** 2009-01-02 9:00 am So, as you can see, the total timespan (calculated e.g. by Da...

How to calculate MySQL Query Memory/CPU Cost

Is there a way/tool/technique to calculate the processor and memory cost of a specific MySQL query? ...

Calculate exponents via addition only

We're writing a very simple program to execute on a processor we've built for a class. It doesn't have the capability to multiply or divide. We do however, had support for addition, subtraction, and, or, and branching for loop control (like branch on equal if you are familiar with MIPS). We were thinking a neat program to run on it would...

looking for ideas on how to calculate project progress percentage

Hi - I'm building a basic app to track progress of projects through db. When the project is first created, it saves the created_at timestamp (yes, it's rails), and the user can then update the following fields: status (choose from 'in progress', 'frozen', 'done' or 'scratched') priority ('none', 'minor', 'major') days (the estimate, in...

asp.net strange calculation result

I have a weird problem in asp.NET when calculating how many hours we need to invoice. In the below example, we have a total of 75,9 hours that need to be invoiced. These hours are spread over several database rows (TimeIDs). Basically, I always deduct the "amount_invoiced" from the "to invoice" hourly count: TimeID:25433 - to invoice...

PropertyChanged affecting several properties

I have a situation where I have a couple of variables who's values depend on each other like this: A is a function of B and C B is a function of A and C C is a function of A and B Either value can change on the UI. I'm doing the calculation and change notification like this: private string _valA; private string _valB; private string _...

Difference between dates in javascript

how to find difference between two dates ...

How to make server real-time calculations more efficient

I am using a mySQL database on a site to calculate the distance of multiple points based on zipcodes, latitudes and longitudes. E.g. Point A at zip code 123456 has latitude 100 and longitude 200 Using a static formula, the distance between Point A and multiple other points are calculated and returned real time. The reference Point A may ...

How to calculate NPV

Hi All i want to calculate the NPV ( Net Present Value ) using PHP. Below is the detail which i have. discount rate : 15% Cash Flow Values per Year: Year 1: 110000 Year 2: 122000 Year 3: 135200 Year 4: 149720 Year 5: 157706 Year 6: 166091 NPV Formula which i am using. NPV = sum of all years( (Ci...

Creating NSDecimal

I am carrying out a number of calculations using NSDecimal and am creating each NSDecimal struct using the following technique: [[NSNumber numberWithFloat:kFloatConstant] decimalValue] I am using NSDecimal to avoid using autoreleased NSDecimalNumber objects (if the NSDecimalNumber approach to accurate calculations is used). However it...

Expression to calculate a field within a loop

I basically have a few variables 0 < na < 250 0 < max <= 16 nb = (na + max - 1) / max n has the following characterstics 0 <= i < nb - 1 => n = max i = nb - 1 => n = na - i * max Is there an easy way to do this without the ternary operator? for (i = 0; i<nb;i++) { n = ((i + 1) * max > na ? na - (i * max) : max); } Examples...

Do math functions of constant expressions get pre-calculated at compile time?

I tend to use math functions of constant expressions for convinience and coherence (i.e log(x)/log(2) instead of log(x)/0.3...). Since these functions aren't actually a part of the language itself, neither are they defined in math.h (only declared), will the constant ones get precalculated at compile time, or will they be wastefully calc...

What is the fastest way to count the unique elements in a list of billion elements?

My problem is not usual. Let's imagine few billions of strings. Strings are usually less then 15 characters. In this list I need to find out the number of the unique elements. First of all, what object should I use? You shouldn't forget if I add a new element I have to check if it is already existing in the list. It is not a problem in ...

Using the Cosine law to calculate distance between 2 points in Objective C?

Hi I get some GPS coordinates from Google Maps and I need to find the distance between them using Objective C. I have implemented the formula but I get results that are way to big. I have tested the values from Google Maps by passing them back into Google Earth and a Geocoding service on the internet and everything checks out. Im now b...

JQuery Calculation returns NaN

I'm using JQuery Calculation plugin with the following script: function recalc() { jQuery("input[class^=percent]").calc( "(invoice - cost)/invoice * 100", { invoice: jQuery("input[class^=invoice]"), cost: jQuery("input[class^=cost]") }, function...