算术问题 [英] Arithmetic problem

查看:65
本文介绍了算术问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在C中有一个非常奇怪的算术问题:


double t = 0.1;

int steps = 10;

double time_step = t /(double)步骤;


我希望time_step的输出为0.01000(我的输出)是形式%5.5f的

,而是一个非常大(且不正确)的负数

数。


任何想法?


谢谢,

Schiz

Hi,

I have a very strange arithmetic problem in C:

double t = 0.1;
int steps = 10;
double time_step = t / (double)steps;

I would expect the output of time_step to be 0.01000 (my output is of
the form %5.5f), but instead it a very large (and incorrect) negative
number.

Any ideas?

Thanks,
Schiz

推荐答案

在文章< ; e5 ********** @ geraldo.cc.utexas.edu>,

Schizoid Man< sc *** @ sf.com>写道:
In article <e5**********@geraldo.cc.utexas.edu>,
Schizoid Man <sc***@sf.com> wrote:
我在C中有一个非常奇怪的算术问题:
I have a very strange arithmetic problem in C:




向我们展示一个有问题的完整程序。


- Richard



Show us a complete program that has the problem.

-- Richard


Richard Tobin写道:
Richard Tobin wrote:
向我们展示一个完整的程序问题。
Show us a complete program that has the problem.




理查德,


这是完整的程序。谢谢。


(这是有问题的模块 - 除了步骤之外传递给这个

函数的任何值,返回一个非常奇数的数字)


#include" stdafx.h"

#include" binomialtree.h"


double treeNPV(double s,double k,double r,double v,double t,int steps)

{

double time_step,up,dn,rt,p,npv;

time_step = t /(double)(步骤);

rt = exp(r * time_step);

up = exp(v * sqrt(time_step) ));

dn = 1 / up;

p =(rt-dn)/(up-dn);


npv = 0.0;


// for(int i = 0; i< = steps; i ++)

npv = v;


返回npv;

}


主要模块:


#include" stdafx .h"

#include" binomialtree.h"


int main(无效)

{

double s,k,r,v,t;

double call_npv;

int步骤;

打印f(输入资产价格S:);

scanf_s("%f"& s);

printf("输入执行价格K) :");

scanf_s("%f",& k);

printf(" Enter riskfree rate R:");

scanf_s("%f",& r);

printf(" Enter volatility V:");

scanf_s(" %f",& v);

printf(输入到期时间T:);

scanf_s("%f",& t );

printf(输入步数:);

scanf_s("%d",& steps);

call_npv = treeNPV(s,k,r,v,t,steps);

printf(" Call option NPV is:%5.5f",call_npv);

scanf_s("%d",& steps);

返回0;

}


头文件:


// stdafx.h:包含标准系统包含文件的文件,


#pragma一次

#定义WIN32_LEAN_AND_MEAN //从Wi中排除很少使用的东西ndows

header

#include< stdio.h>

#include< tchar.h>


// binomialtree.h

#include< stdio.h>

#include< stdlib.h>

# include< math.h>

#define pi 3.141592653589793238

double treeNPV(double s,double k,double r,double v,double t,int步骤);



Hi Richard,

Here''s the complete program. Thank you.

(This is the problematic module - any of the values passed to this
function apart from steps, returns a very odd number)

#include "stdafx.h"
#include "binomialtree.h"

double treeNPV(double s, double k, double r, double v, double t, int steps)
{
double time_step, up, dn, rt, p, npv;
time_step = t / (double)(steps);
rt = exp(r*time_step);
up = exp(v*sqrt(time_step));
dn = 1/up;
p = (rt-dn)/(up-dn);

npv = 0.0;

//for (int i=0; i<=steps; i++)
npv = v;

return npv;
}

Main modules:

#include "stdafx.h"
#include "binomialtree.h"

int main(void)
{
double s, k, r, v, t;
double call_npv;
int steps;
printf("Enter asset price S: ");
scanf_s("%f", &s);
printf("Enter strike price K: ");
scanf_s("%f", &k);
printf("Enter riskfree rate R: ");
scanf_s("%f", &r);
printf("Enter volatility V: ");
scanf_s("%f", &v);
printf("Enter time to expiry T: ");
scanf_s("%f", &t);
printf("Enter number of steps: ");
scanf_s("%d", &steps);
call_npv = treeNPV(s,k,r,v,t,steps);
printf("Call option NPV is: %5.5f", call_npv);
scanf_s("%d", &steps);
return 0;
}

Header files:

// stdafx.h : include file for standard system include files,

#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows
headers
#include <stdio.h>
#include <tchar.h>

// binomialtree.h
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi 3.141592653589793238

double treeNPV(double s, double k, double r, double v, double t, int steps);


在文章< e5 ********** @ geraldo.cc.utexas.edu>,

Schizoid Man< sc *** @ sf.com>写道:
In article <e5**********@geraldo.cc.utexas.edu>,
Schizoid Man <sc***@sf.com> wrote:
这是完整的程序。谢谢。
scanf_s("%f",& s);


我不知道scanf_s是什么。它不是标准的C函数。

#include< tchar.h>
Here''s the complete program. Thank you. scanf_s("%f", &s);
I don''t know what scanf_s is. It''s not a standard C function.
#include <tchar.h>




我不知道是什么tchar.h是。


但是如果scanf_s类似于scanf,那么你应该使用格式%lf

来读取双精度数。


- Richard



And I don''t know what tchar.h is.

But if scanf_s is like scanf, then you should be using format %lf
to read into a double.

-- Richard


这篇关于算术问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆