mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Updated nbodies. Fixed sum/product on floats.
This commit is contained in:
@@ -349,8 +349,8 @@ macro double double.round(double x) = $$round(x);
|
||||
macro double double.roundeven(double x) = $$roundeven(x);
|
||||
macro double double.trunc(double x) = $$trunc(x);
|
||||
|
||||
macro double double[<*>].sum(double[<*>] x, double start = 0.0) = $$reduce_add(x, start);
|
||||
macro double double[<*>].product(double[<*>] x, double start = 1.0) = $$reduce_mul(x, start);
|
||||
macro double double[<*>].sum(double[<*>] x, double start = 0.0) = $$reduce_fadd(x, start);
|
||||
macro double double[<*>].product(double[<*>] x, double start = 1.0) = $$reduce_fmul(x, start);
|
||||
macro double double[<*>].max(double[<*>] x) = $$reduce_fmax(x);
|
||||
macro double double[<*>].min(double[<*>] x) = $$reduce_fmin(x);
|
||||
macro double[<*>] double[<*>].ceil(double[<*>] x) = $$ceil(x);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
module nbodies;
|
||||
import std::io;
|
||||
import std::math;
|
||||
|
||||
const PI = 3.141592653589793;
|
||||
const SOLAR_MASS = 4 * PI * PI;
|
||||
@@ -24,7 +26,7 @@ fn void advance(Planet[] bodies) @noinline
|
||||
double dx = b.x - b2.x;
|
||||
double dy = b.y - b2.y;
|
||||
double dz = b.z - b2.z;
|
||||
double inv_distance = 1.0/sqrt(dx * dx + dy * dy + dz * dz);
|
||||
double inv_distance = 1.0 / math::sqrt(dx * dx + dy * dy + dz * dz);
|
||||
double mag = inv_distance * inv_distance * inv_distance;
|
||||
b.vx -= dx * b2.mass * mag;
|
||||
b.vy -= dy * b2.mass * mag;
|
||||
@@ -56,7 +58,7 @@ fn double energy(Planet[] bodies)
|
||||
double dx = b.x - b2.x;
|
||||
double dy = b.y - b2.y;
|
||||
double dz = b.z - b2.z;
|
||||
double distance = sqrt(dx * dx + dy * dy + dz * dz);
|
||||
double distance = math::sqrt(dx * dx + dy * dy + dz * dz);
|
||||
e -= (b.mass * b2.mass) / distance;
|
||||
}
|
||||
}
|
||||
@@ -139,23 +141,20 @@ fn void scale_bodies(Planet[] bodies, double scale)
|
||||
}
|
||||
}
|
||||
|
||||
extern fn int atoi(char *s);
|
||||
extern fn int printf(char *s, ...);
|
||||
extern fn double sqrt(double);
|
||||
|
||||
fn int main(int argc, char ** argv)
|
||||
fn void main(char[][] args)
|
||||
{
|
||||
int n = atoi(argv[1]);
|
||||
int n = args.len < 2 ? 50000000 : str::to_int(args[1])!!;
|
||||
|
||||
Planet[] bodies = &planet_bodies;
|
||||
offset_momentum(bodies);
|
||||
printf ("%.9f\n", energy(bodies));
|
||||
double start = energy(bodies);
|
||||
scale_bodies(bodies, DT);
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
advance(bodies);
|
||||
}
|
||||
scale_bodies(bodies, RECIP_DT);
|
||||
printf ("%.9f\n", energy(bodies));
|
||||
return 0;
|
||||
io::printfln("%.9f", start);
|
||||
io::printfln("%.9f", energy(bodies));
|
||||
}
|
||||
|
||||
@@ -4634,6 +4634,9 @@ static bool sema_expr_analyse_div(SemaContext *context, Expr *expr, Expr *left,
|
||||
case CONST_FLOAT:
|
||||
// This is allowed, as it will generate a NaN
|
||||
break;
|
||||
case CONST_INITIALIZER:
|
||||
// Do not analyse
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define COMPILER_VERSION "0.4.0"
|
||||
#define COMPILER_VERSION "0.4.1"
|
||||
Reference in New Issue
Block a user