Serving the Quantitative Finance Community

 
User avatar
topkatz
Topic Author
Posts: 0
Joined: October 4th, 2005, 8:57 pm

Multiply or Divide?

December 3rd, 2007, 7:52 pm

Here's a simple straightforward question that I have some general ideas about, but I'm interested in the opinions of all you number jockeys. Suppose I'm converting a quantity from one system of units to another, e.g., milliseconds to years. I can either multiply my millisecond quantity by 3.170979198e-11 (and I can get greater accuracy than this, of course) or divide by 31,536,000,000. (This is obviously an extreme example.) For what ranges of your quantity to convert would you use one method over the other? Would you do anything different depending on the language and / or processor? Thanks!-- TMK --212-460-5430 home917-656-5351 cell
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Multiply or Divide?

December 3rd, 2007, 8:31 pm

The boost library might be useful since it has a library with traits for conversions from built-in types and user types. boost numeric conversionIs this the idea? You can do it code yourself but these small/large numbers need to be checked and I would let the compiler do that.
Last edited by Cuchulainn on December 2nd, 2007, 11:00 pm, edited 1 time in total.
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Multiply or Divide?

December 3rd, 2007, 8:45 pm

To my knowledge and on all off-the-shelf hardware, multiplication is always faster than division assuming you can precompute and cache the reciprocal of the number you would have divided by. On an Intel Pentium, a division is something like 39-42 clock cycles, and a multiply is 3/1 to 7/4 depending on whether its floating point or integer and latency or throughput. The only exception is if the conversion factor is a power of two (e.g., pints to gallons) in which case, the time is invariant assuming you have the code to recognize that special case.
 
User avatar
Alan
Posts: 3050
Joined: December 19th, 2001, 4:01 am
Location: California
Contact:

Multiply or Divide?

December 3rd, 2007, 9:04 pm

You should arrange your computation so that it occurs in natural units, meaning that most of the stuff appearing is O(1).Then, when you present your results, you should use the units common to your audience, -and- explain them in detail, anyway.A pet peeve of mine is finance papers showing *dimensionful* quantities, like volatility (it's a square root of a rate, guys),presenting it as dimensionless, and then never properly explaining the (dropped) units, (oh yeah, we meant: per day x 10000). regards,
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Multiply or Divide?

December 4th, 2007, 7:50 am

QuoteOriginally posted by: AlanA pet peeve of mine is finance papers showing *dimensionful* quantities, like volatility (it's a square root of a rate, guys),presenting it as dimensionless, and then never properly explaining the (dropped) units, (oh yeah, we meant: per day x 10000). I have seen three different interpretations of Heston parameters, very confusing (the best one was exp(q) instead of q!)Not only is O(1) a good idea but also the standardised O(1), that is q and not exp(q).Dimensional Analysis, what?QuoteA failure to recognize and correct an error in a transfer of information between the Mars Climate Orbiter spacecraft team in Colorado and the mission navigation team in California led to the loss of the spacecraft last week, preliminary findings by NASA's Jet Propulsion Laboratory internal peer review indicate. "People sometimes make errors," said Dr. Edward Weiler, NASA's Associate Administrator for Space Science. "The problem here was not the error, it was the failure of NASA's systems engineering, and the checks and balances in our processes to detect the error. That's why we lost the spacecraft." The peer review preliminary findings indicate that one team used English units (e.g., inches, feet and pounds) while the other used metric units for a key spacecraft operation. This information was critical to the maneuvers required to place the spacecraft in the proper Mars orbit.
Last edited by Cuchulainn on December 3rd, 2007, 11:00 pm, edited 1 time in total.
 
User avatar
quartz
Posts: 3
Joined: June 28th, 2005, 12:33 pm

Multiply or Divide?

December 4th, 2007, 10:42 am

Division is often implemented in HW through multiplications and sums, so it can hardly be faster in general. Accuracy should be equal under sw "emulation" and not differ much in the inverse precalculation case (but personally didnt bother testing for accuracy), except maybe for a few less significant bits (Intel FPUs work internally at 80 bit, so if you manage to keep everything in the internal 8 registers something might be gained with division over the reciprocal multiplication, but not for usual coding i'd say).As for speed, P4 was even worse than pentium, with a throughput of 2 cycles for a mul vs 43 for div. Things get better with SSEx though... and the latest processors have finally faster divs. AMD has long had much better division, that's why it had some lead in scientific applications, but dont know about the latest models.
Last edited by quartz on December 3rd, 2007, 11:00 pm, edited 1 time in total.