我正在寻找获得π值的最快方法,作为个人挑战.更具体地说,我使用的方法不涉及使用#define
常量M_PI
,或者对数字进行硬编码.
下面的程序测试了我所知道的各种方式.从理论上讲,内联汇编版本是最快的选择,但显然不便于携带.我已将其作为基线与其他版本进行比较.在我的测试中,使用内置4 * atan(1)
函数,在GCC 4.2上版本最快,因为它会自动将其折叠atan(1)
为常量.根据-fno-builtin
指定,atan2(0, -1)
版本最快.
这是主要的测试程序(pitimes.c
):
#include <math.h>
#include <stdio.h>
#include <time.h>
#define ITERS 10000000
#define TESTWITH(x) { \
diff = 0.0; \
time1 = clock(); \
for (i = 0; i < ITERS; ++i) \
diff += (x) - M_PI; \
time2 = clock(); \
printf("%s\t=> %e, time => %f\n", #x, diff, diffclock(time2, time1)); \
}
static inline double
diffclock(clock_t time1, clock_t time0)
{ …
Run Code Online (Sandbox Code Playgroud) 当我在JS中使用数学时,我希望它的trig函数使用度数值而不是弧度值.我该怎么办?
当尝试在我的编程语言中使用三角函数时,我得到了一个看似非常错误的结果。
例如,
sin(90) = 0.8939966636005579
Run Code Online (Sandbox Code Playgroud)
但我希望sin(90)
是1
。其他三角函数也会发生类似的情况。
这是怎么回事?我能做些什么来解决这个问题?
这是三角函数输入单位要求的规范问题。(meta上的相关讨论)
我正在为移动设备制作一个"指南针".我有以下几点:
point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
point 2 (target location): Latitude = 50.9246, Longitude = 10.2257
Run Code Online (Sandbox Code Playgroud)
另外我有以下信息(来自我的android手机):
The compass-direction in degree, wich bears to the north.
For example, when I direct my phone to north, I get 0°
Run Code Online (Sandbox Code Playgroud)
我怎样才能创建一个"指南针"箭头,向我展示指向方向的方向?
这有数学问题吗?
谢谢!
编辑:好的,我找到了一个解决方案,它看起来像这样:
/**
* Params: lat1, long1 => Latitude and Longitude of current point
* lat2, long2 => Latitude and Longitude of target point
*
* headX => x-Value of built-in phone-compass
*
* Returns …
Run Code Online (Sandbox Code Playgroud) math ×2
algorithm ×1
degrees ×1
direction ×1
geolocation ×1
javascript ×1
location ×1
performance ×1
pi ×1
radians ×1
trigonometry ×1
unix ×1