在for循环中添加double值 [英] adding double value in for loop

查看:367
本文介绍了在for循环中添加double值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在循环中使用double值并像这样输出! 0.00020

0.00040

0.00060



我正在研究mql4中的交易程序,就像c一样,如果价格变动应该买20点交易,如果价格下跌20点交易应该卖出。应该继续点到点。



I want to use double value in loop and get out put like this !! 0.00020
0.00040
0.00060

I am working on trading program in mql4 its like c , if price goes up 20points trade should buy and if price goes down 20 points trade should sell. it should be continue point to point.

double Level1 = Open;                            // ticket1=OrderSend(Symbol(),1,Lots,Bid,3,0,Bid-TakeProfit*Point,"jems");
double i = 0.00020;
double j = 0.00020;
int ticket1;
int ticket2;
int Total = OrdersTotal();
int Prd = Period();
double Price = Bid;
double value1 = 0.00020;

if(Price > Level1 + value1 )

{

for (i = 0.00000 ;Level1 + i < Price; i++ ) 

         {  
         i = i + 0.00020;
          Sleep(10000);                 
          Alert("Buy",i);            
       
            }
        
           
}
   else if (Price < Level1 - value1)
{

for (j = 0.00000 ; Level1 - j < Price ; j++) 
      {                   
        j = j + 0.00020;
         Sleep(10000);
          Alert("sell",j);            
        
        }
    return(0);
    }
    
    }

推荐答案

#include <stdio.h>

printf("value: %.5f", 0.02);





注意:使用类似BigDecimal的类或固定点类(比例因子为10 ^ x )而不是double或float在这里更合适,因为你可以将浮点计算的误差减少到零。严肃的银行应用程序通常使用BigDecimal库。





编辑:一些有用的链接:

- 取决于使用定点类而不是double / float的问题就足够了:定点运算 [< a href =http://en.wikipedia.org/wiki/Fixed-point_arithmetictarget =_ blanktitle =New Window> ^ ]。如果分数中的固定位数不适合你,则定点不是一个好的解决方案。



- 如果定点算术不是'那么你应该使用一个库来实现本页表格中列出的decimal32,decimal64或decimal128浮点类型: IEEE浮点 [ ^ ]。 decimalXXX类型也是浮点类型,如binary32(float)和binary(64)double,但它们使用10作为指数的基数而不是2,因此它们可以在指定范围内准确表示所有十进制数。与定点类型不同,您可以使用float / double移动小数点。



- 如果小数实现的范围不够,那么你可以选择一个可以准确存储任意大十进制数的BigDecimal库。银行应用程序使用此解决方案来计算每个帐户的金额。



Note: The usage of a BigDecimal-like class or a fixedpoint class (with a scale factor of 10^x) instead of double or float would be more appropriate here because with that you would be able to reduce error coming from floating point calculations to zero. Serious banking applications often make use of BigDecimal libraries.


Some useful links:
- Depending on your problem using a fixed-point class instead of double/float may be enough: Fixed-point arithmetic[^]. If a fixed number of digits in the fraction isn't suitable for you then fixed-point isn't a good solution.

- If fixed-point arithmetic isn't enough then you should use a library that implements the decimal32, decimal64, or decimal128 floating-point types listed in a table on this page: IEEE floating point[^]. decimalXXX types are also floating point types like binary32(float) and binary(64)double but they use 10 as the base of the exponent instead of 2 so they can represent all decimal numbers accurately within a specified range. Unlike with fixed point types you can move the decimal point as you can with float/double.

- If the range of the decimal implementation isn't enough then you can choose a BigDecimal library that can store arbitrarily large decimal numbers accurately. Banking applications use this solution to make calculations with the amount of money on each account.


重新设计您的事件驱动概念的解决方案。在这种情况下,你有一个新的qoute功能,你可以做出决定。



不要使用Sleep !!!



概念元代码



redesign your solution to "event driven" concept. In that case you have a function which gets a new qoute and in that youte you make your decision.

Do NOT use Sleep!!!

conceptual Metacode

int OnNewQoute( double qoute )
{
  if( qoute > sellLimit )
  {
     doSellAction();
     return 1;
  }

  return 0;
}


你好,

喜欢 pasztorpisti 说,增加你的分辨率数字为了使用sych非常低的浮点数,



例如:0.00020与0.20 / 1000相同,

这就像更换相机的镜头,以便专注于一个非常小的物体

i认为你可以通过这种方式返回正确的值



祝你好运,

z3ngew
hi there,
Like pasztorpisti said, increase the resolution of your number to in order to work with sych very low floating point number,

for example: 0.00020 is the same as 0.20 / 1000,
it is like change the lens of your camera in order to focus on a very small object
i think you can return correct values this way

Good Luck,
z3ngew


这篇关于在for循环中添加double值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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