core calculation translation from Java to Objective-C. not tested in any way.