| | |
float and double behavior
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
I am trying to optimize code for Monte Carlo simulation. Even minute performance differences piles up after 100 million iterations and thus I need to squeeze every nanosecond from math operations!
One area where I thought I could save a lot stems from the fact that I only require precision of 4 significant digits. It therefore seems natural to use float rather than double.
However, some testing suggests that double still performs better! This is unexpected.
Why is it that despite the fact that float is 32 bits and double 64 bits, mathh functions are quicker to perform exp(double) and pow(double, double) than exp(float) and pow(float, float) (or even expf and powf)? Here is some code...
One area where I thought I could save a lot stems from the fact that I only require precision of 4 significant digits. It therefore seems natural to use float rather than double.
However, some testing suggests that double still performs better! This is unexpected.
Why is it that despite the fact that float is 32 bits and double 64 bits, mathh functions are quicker to perform exp(double) and pow(double, double) than exp(float) and pow(float, float) (or even expf and powf)? Here is some code...
C++ Syntax (Toggle Plain Text)
#include <math.h> #include <iostream> #include "Timer.h" int main() { double a = 23.14; float c = 23.14; Timer t; t.tic(); for (int i = 0; i < 10000000; i++) expf(c); cout<<"expf(float) returns " << expf(c)<<" and took "<<t.toc()<< " seconds." << endl; t.tic(); for (int i = 0; i < 10000000; i++) exp(c); cout<<"exp(float) returns " << exp(c)<<" and took "<<t.toc()<< " seconds." << endl; t.tic(); for (int i = 0; i < 10000000; i++) exp(a); cout<<"exp(double) returns " << exp(a)<<" and took "<<t.toc()<< " seconds." << endl; }
-7
#2 21 Days Ago
>>However, some testing suggests that double still performs better! This is unexpected.
Yup. floats are always converted to doubles when used as function parameters. Other factors may influence it too, such as the math coprocessor on your computer.
Yup. floats are always converted to doubles when used as function parameters. Other factors may influence it too, such as the math coprocessor on your computer.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
1
#3 21 Days Ago
•
•
•
•
Yup. floats are always converted to doubles when used as function parameters.
•
•
•
•
No, that's only true if
(1) you call the function without a prototype in scope, or
(2) it's a variable argument to a variadic function like 'printf'.
Last edited by Dave Sinkula; 21 Days Ago at 12:59 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
0
#5 21 Days Ago
floats are made for spaced optimization, it does not necessarily have to
be faster than double, especially in a 64bit CPU.
be faster than double, especially in a 64bit CPU.
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
0
#6 21 Days Ago
How about you show some code, so we can try to help you,
although monte carlo problem should be slow to execute as it is quite
easy to implement.
although monte carlo problem should be slow to execute as it is quite
easy to implement.
Last edited by firstPerson; 21 Days Ago at 7:58 pm.
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
![]() |
Similar Threads
- error: invalid operands to binary ^ (have ‘float’ and ‘double’) (C)
- Difference between float and double (C++)
- Conversion of Float/Double to binary (Java)
- using float, double numbers in mips assembly language (Assembly)
- C++: problam with data type(int,float,double) (C++)
- Float/Double to String conversion (C)
- problem with Float and Double (C)
Other Threads in the C++ Forum
- Previous Thread: very basic c++ question on vectors
- Next Thread: waveOutWrite is creating complex wave instead of simple sine wave
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






