Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 6, 2013, 02:14 PM   #26
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Senor Cuete View Post
"math" is a collective noun so there is no such thing as "maths".
There's also the subject of geography.

You see, there is this little island in the North Sea, called Great Britain. And the people living on that little island speak proper English aka British English. And when the do maths, they do maths. Not math.
gnasher729 is offline   1 Reply With Quote
Old Sep 6, 2013, 04:14 PM   #27
Senor Cuete
macrumors regular
 
Join Date: Nov 2011
Quote:
Originally Posted by gnasher729 View Post
There's also the subject of geography.

You see, there is this little island in the North Sea, called Great Britain. And the people living on that little island speak proper English aka British English. And when the do maths, they do maths. Not math.
According to the dictionary "math" is a mid 19th century abbreviation of the word "mathematics" a plural noun usually treated as singular. So yes, I studied math and also English, but not British English slang.
Senor Cuete is offline   0 Reply With Quote
Old Sep 6, 2013, 10:20 PM   #28
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
If the British speak English it is certainly not proper English.
lloyddean is offline   0 Reply With Quote
Old Sep 7, 2013, 09:01 AM   #29
Qaanol
macrumors 6502a
 
Join Date: Jun 2010
Back on topic, I did a quick test of vvsincos and vvsincosf on my mid-2007 MBP, where I found the float version is about 3-4 times faster than the double version when processing the same number of values.

Does anyone with a more recent machine want to test the same thing?

Last edited by Qaanol; Sep 7, 2013 at 09:48 AM.
Qaanol is offline   0 Reply With Quote
Old Sep 7, 2013, 10:14 AM   #30
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by Qaanol View Post
Back on topic, I did a quick test of vvsincos and vvsincosf on my mid-2007 MBP, where I found the float version is about 3-4 times faster than the double version when processing the same number of values.

Does anyone with a more recent machine want to test the same thing?
If you want to post your code I can run on a 2012 and 2013 rMBP.
mrichmon is offline   0 Reply With Quote
Old Sep 7, 2013, 03:09 PM   #31
Qaanol
macrumors 6502a
 
Join Date: Jun 2010
Here’s my test code, written as a command-line utility (be sure to compile for “release”, not “debug”, so it will run at full-speed in Terminal.) Be sure to link the Accelerate framework as well. There is one optional command-line argument to specify the size of the arrays. If omitted, the default is 10,000.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <vecLib/vecLib.h>

int main (int argc, const char * argv[]) {
	int n = 10000;
	if (argc > 1) {
		n = atoi(argv[1]);
		if (n < 1) {
			printf("First argument must be positive\n");
			return 1;
		}
	}
	time_t t[3];
	float *f1 = malloc(n * sizeof(float));
	float *f2 = malloc(n * sizeof(float));
	double *d1 = malloc(n * sizeof(double));
	double *d2 = malloc(n * sizeof(double));
	srandomdev();
	for (int ii = 0; ii < n; ii = ii + 1) {
		d1[ii] = (double)random();
		f1[ii] = (float)random();
	}
	t[0] = clock();
	vvsincosf(f1, f2, f1, &n);
	t[1] = clock();
	vvsincos(d1, d2, d1, &n);
	t[2] = clock();
	double t1 = (double)(t[1] - t[0])/CLOCKS_PER_SEC;
	double t2 = (double)(t[2] - t[1])/CLOCKS_PER_SEC;
	printf("%f seconds for %i floats\n", t1, n);
	printf("%f seconds for %i doubles\n", t2, n);
	printf("float is %f times the speed of double\n", t2/t1);
	return 0;
}
Here is a typical result on my machine (though to be fair I ran this while streaming video online and the computer was running hot, so who knows what that did to my performance.)

Code:
0.577504 seconds for 16777216 floats
2.182337 seconds for 16777216 doubles
float is 3.778912 times the speed of double

Last edited by Qaanol; Sep 7, 2013 at 03:27 PM.
Qaanol is offline   0 Reply With Quote
Old Sep 7, 2013, 05:31 PM   #32
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Qaanol View Post
Here’s my test code, written as a command-line utility (be sure to compile for “release”, not “debug”, so it will run at full-speed in Terminal.) Be sure to link the Accelerate framework as well. There is one optional command-line argument to specify the size of the arrays. If omitted, the default is 10,000.
There's a problem with the code that makes the results practically meaningless.

You store the result of random () to be used as the argument of the sine and cosine functions. The values returned by random are in the range from about 0 to 2 billion. That's absolutely non-typical for the arguments of sine and cosine. Worse, when you store a value between 1 and 2 billion into a float, the resolution is 128. That means, the lowest bit has a value of 128, and the difference between two consecutive float numbers is 128. The period of sine and cosine is 2pi. 128 is more than 20 times that period, so for float the actual arguments are totally meaningless. vvsincosf might as well just return 0 and 1 for the sine and cosine for these large values. For double, that's not the case; the arguments even in that huge range still have a resolution of 1/8million radians.

You'd get much more meaningful results if you scaled the values lets say into the interval [-2, +2].

----------

Quote:
Originally Posted by Senor Cuete View Post
According to the dictionary "math" is a mid 19th century abbreviation of the word "mathematics" a plural noun usually treated as singular. So yes, I studied math and also English, but not British English slang.
"Maths" hasn't been slang for the last 100 years.
gnasher729 is offline   0 Reply With Quote
Old Sep 7, 2013, 06:48 PM   #33
Qaanol
macrumors 6502a
 
Join Date: Jun 2010
Quote:
Originally Posted by gnasher729 View Post
There's a problem with the code that makes the results practically meaningless.

You store the result of random () to be used as the argument of the sine and cosine functions. The values returned by random are in the range from about 0 to 2 billion. That's absolutely non-typical for the arguments of sine and cosine. Worse, when you store a value between 1 and 2 billion into a float, the resolution is 128. That means, the lowest bit has a value of 128, and the difference between two consecutive float numbers is 128. The period of sine and cosine is 2pi. 128 is more than 20 times that period, so for float the actual arguments are totally meaningless. vvsincosf might as well just return 0 and 1 for the sine and cosine for these large values. For double, that's not the case; the arguments even in that huge range still have a resolution of 1/8million radians.

You'd get much more meaningful results if you scaled the values lets say into the interval [-2, +2].
Okay, here is a typical run of my original code without a lot of other programs running:
Code:
0.210407 seconds for 16777216 floats
0.773957 seconds for 16777216 doubles
float is 3.678380 times the speed of double
And here is after updating the code:
Code:
0.214049 seconds for 16777216 floats
0.782355 seconds for 16777216 doubles
float is 3.655028 times the speed of double
The updated code is identical except the assignments inside the for loop are now:
Code:
d1[ii] = 6.2832 * ((double)random() / RAND_MAX);
f1[ii] = (float)(6.2832 * ((double)random() / RAND_MAX));
The times are usually within a couple milliseconds over many runs, so I’d say the result of the test doesn’t change.

Quote:
Originally Posted by gnasher729 View Post
"Maths" hasn't been slang for the last 100 years.
This is false. If you go to a major university, even in the USA, and spend some time in the math department graduate lounge, you’ll hear plenty of people refer to their subjects of study as ‘maths’.
Qaanol is offline   0 Reply With Quote
Old Sep 10, 2013, 03:31 AM   #34
Mac_Max
macrumors 6502
 
Join Date: Mar 2004
Quote:
Originally Posted by Qaanol View Post
This is false. If you go to a major university, even in the USA, and spend some time in the math department graduate lounge, you’ll hear plenty of people refer to their subjects of study as ‘maths’.
I think Gnasher's point was that it's not slang because it is a correct word...

Blimy, colour me queer, it's as if the United States are not speaking the same language. (I apologize to all the Brits and Aussies in the room).

For what it's worth:

http://en.wikipedia.org/wiki/Compari...ritish_English
http://www.dailywritingtips.com/math-or-maths/

You're all correct.

One thing that always bothers me when I hear British English is the treatment of collective noun - verb agreements. Jeremy Clarkson in particular likes to strongly inflect "have" for dramatic effect.

I.e.

(British) "Germany have won the competition."

v.s.

(US) "Germany has won the competition."

Damn Brits, can't they speak their own language? Back to Math/Maths... It kinda makes sense when you look at the difference in subject-verb agreement between British and US english.

(British) "Maths are difficult."

(US) "Math is difficult."

Both sound reasonable. Swapping our nation's respective agreements...

"Math are difficult."

"Maths is difficult"

Not particularly poetic.
Mac_Max is offline   0 Reply With Quote
Old Sep 10, 2013, 04:17 AM   #35
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Qaanol View Post
This is false. If you go to a major university, even in the USA, and spend some time in the math department graduate lounge, you’ll hear plenty of people refer to their subjects of study as ‘maths’.
Missing the point. In the UK, "maths" isn't slang. It is the proper abbreviation for mathematics.


Quote:
Originally Posted by Qaanol View Post
The times are usually within a couple milliseconds over many runs, so I’d say the result of the test doesn’t change.
The point was that you started with totally non-typical value, so you couldn't know whether your results applied to real-world use or not. Seems they do (which is disappointing, since I would have expected shortcuts to calculate sine / cosine faster for typical values), but it still needed verification.
gnasher729 is offline   0 Reply With Quote
Old Sep 11, 2013, 09:13 AM   #36
Qaanol
macrumors 6502a
 
Join Date: Jun 2010
Quote:
Originally Posted by Mac_Max View Post
I think Gnasher's point was that it's not slang because it is a correct word...
Quote:
Originally Posted by gnasher729 View Post
Missing the point. In the UK, "maths" isn't slang. It is the proper abbreviation for mathematics.
Yep, I missed that the first time.

Quote:
Originally Posted by gnasher729 View Post
The point was that you started with totally non-typical value, so you couldn't know whether your results applied to real-world use or not. Seems they do (which is disappointing, since I would have expected shortcuts to calculate sine / cosine faster for typical values), but it still needed verification.
I'm actually quite glad that the vectorized functions do not make special optimizations for atypical values. That would require testing every single entry to see if the atypical conditions were met, which the vast majority of the time would not be because they are atypical, so the effect would just be to slow down the processing of typical values.
Qaanol is offline   0 Reply With Quote
Old Sep 11, 2013, 03:43 PM   #37
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Qaanol View Post
Yep, I missed that the first time.


I'm actually quite glad that the vectorized functions do not make special optimizations for atypical values. That would require testing every single entry to see if the atypical conditions were met, which the vast majority of the time would not be because they are atypical, so the effect would just be to slow down the processing of typical values.
Excuse me, but that's not how it works. You don't make optimizations for atypical values, you make optimizations for typical values (which possibly isn't happening).

Every implementation of sine / cosine transforms the argument into the range from -pi/4 to +pi/4. In the general case, you multiply the argument by pi/2, round to the nearest integer, multiply by pi/2 _very_ carefully to avoid rounding errors and subtract that from the argument. That's expensive. In the typical case, say -1.25 pi < x < 1.25 pi, all you do is add or subtract pi or pi/2 or leave the argument unchanged. A lot faster. But if you want to do this you _must_ check for the atypical case or get nonsense results.

BTW. Lots of MacOS X / iOS graphics code uses the type CGFloat. On the iPhone 5s, they now use CGFloat = double.
gnasher729 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
My sin is Pride. What is yours? jack170794 iPhone and iPod touch Apps 0 Aug 6, 2013 10:53 PM
The one Application nearly a SIN not to have on your Mac MacGuru100 Mac Applications and Mac App Store 2 Jun 23, 2013 01:44 AM

Forum Jump

All times are GMT -5. The time now is 03:31 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC