小编use*_*735的帖子

matlab和c与cos函数不同

我有一个在matlab中实现的程序和c中的相同程序,结果不同.

我有点困惑的是cos函数没有返回完全相同的结果.

在这两种情况下,我使用相同的计算机,Intel Core 2 Duo和8字节双数据类型.

为什么结果不同?

这是测试:

c:
double a = 2.89308776595231886830;
double b = cos(a);
printf("a = %.50f\n", a);
printf("b = %.50f\n", b);
printf("sizeof(a): %ld\n", sizeof(a));
printf("sizeof(b): %ld\n", sizeof(b));

a = 2.89308776595231886830106304842047393321990966796875
b = -0.96928123535654842068964853751822374761104583740234
sizeof(a): 8
sizeof(b): 8



matlab:
a = 2.89308776595231886830
b = cos(a);
fprintf('a = %.50f\n', a);
fprintf('b = %.50f\n', b);
whos('a')
whos('b')

a = 2.89308776595231886830106304842047393321990966796875
b = -0.96928123535654830966734607500256970524787902832031
  Name      Size            Bytes  Class     Attributes
  a         1x1                 8  double              

  Name      Size            Bytes  Class     Attributes
  b         1x1 …
Run Code Online (Sandbox Code Playgroud)

c floating-point precision matlab

8
推荐指数
2
解决办法
3039
查看次数

env-> findClass的( "java.lang.Math中");

env->FindClass("java.lang.Math");失败.为什么?

gcc -I/System/Library/Frameworks/JavaVM.framework/Headers test.cpp -framework JavaVM -o test && ./test
Run Code Online (Sandbox Code Playgroud)

http://developer.apple.com/library/mac/#samplecode/simpleJavaLauncher/Listings/utils_h.html#//apple_ref/doc/uid/DTS10000688-utils_h-DontLinkElementID_7 http://developer.apple.com/library/ MAC /#技术说明/ TN2147/_index.html

#include <jni.h>
#include <stdlib.h>

int main() {
    printf("START.\n");

    JavaVM* jvm = NULL;
    JNIEnv *env;

    JavaVMInitArgs vm_args;
    JNI_GetDefaultJavaVMInitArgs(&vm_args); 
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 0;

    int ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if(ret < 0) {
        printf("Unable to Launch JVM\n");       
    return 1;
    }

    jclass mathClass = env->FindClass("java.lang.Math"); 
    if (mathClass == NULL) {
        printf("Unable to find java.lang.Math\n");
    return 1;
    }

    jmethodID cosMethod = env->GetStaticMethodID(mathClass, "cos", "(D)D");
    if (cosMethod …
Run Code Online (Sandbox Code Playgroud)

c java

1
推荐指数
1
解决办法
1925
查看次数

标签 统计

c ×2

floating-point ×1

java ×1

matlab ×1

precision ×1