Last active
December 9, 2023 17:48
-
-
Save assyrianic/c6f4b4da416d7af4fe9cde9eee4ab267 to your computer and use it in GitHub Desktop.
simpsons rule for SourcePawn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stock float simpsons_func(float x) { | |
return 0.0; /// replace with math operations desired. | |
} | |
/** | |
* Composite Simpson's Rule: approximates the area of a function. | |
* 'a' represents the lower bounds | |
* 'b' represents the upper bounds | |
* 'num_intervals' is for how many iterations needed (this is for increasing accuracy) | |
*/ | |
stock float simpsons_rule_comp(float a, float b, int num_intervals) { | |
float h = (b - a) / num_intervals; | |
float result = simpsons_func(a) + simpsons_func(b); | |
for( int i = 1; i < num_intervals; i += 2 ) { | |
result += 4.0 * simpsons_func(a + i * h); | |
} | |
for( int i = 2; i < num_intervals - 1; i += 2 ) { | |
result += 2.0 * simpsons_func(a + i * h); | |
} | |
enum{ recip_of_3 = 0x3eaaaaab }; | |
return result * h * view_as< float >(recip_of_3); | |
} | |
/** | |
* Simpson's Rule: approximates the area of a function. | |
* 'a' represents the lower bounds | |
* 'b' represents the upper bounds | |
*/ | |
stock float simpsons_rule(float a, float b) { | |
float result = simpsons_func(a) + 4.0 * simpsons_func((a+b) * 0.5) + simpsons_func(b); | |
enum{ recip_of_6 = 0x3e2aaaab }; | |
return result * (b - a) * view_as< float >(recip_of_6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment