Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json.mqh 的 parseInt 是否該做以下修正? #57

Closed
OmarHung opened this issue Oct 16, 2021 · 3 comments
Closed

Json.mqh 的 parseInt 是否該做以下修正? #57

OmarHung opened this issue Oct 16, 2021 · 3 comments
Assignees
Labels

Comments

@OmarHung
Copy link

Mql\Format\Json.mqh

bool     parseInt(int &sign,int &value)
     {
      sign=1;
      unichar c=m_stream.nextChar();
      if(c=='-')
        {
         sign=-1;
         c=m_stream.nextChar();
        }
      value=0;
      if(c=='0') // 0
        {
         // sign=1; // 上面預設就是sign=1這裡還做sign=1會把負數變為正
         return true;
        }
      else if(c<='9' && c>='1') // onenine 0..9
        {
         do
           {
            value*=10;
            value+=(int)(c-'0');
            c=m_stream.nextChar();
           }
         while(c<='9' && c>='0');
         m_stream.pushChar(c);
         return true;
        }
      else
        {
         return false;
        }
     }
@dingmaotu
Copy link
Owner

我想这里不需要改,因为这里判断的是首个或者第二个字符即是0的情况,也就是“-0”,或者“0”,那么就直接返回。这里假设十进制整数的第一个数字不能为0(因为这通常用于表达8进制)

@OmarHung
Copy link
Author

OmarHung commented Sep 8, 2023

但若數據為負數,最後會變成正數,例如 -0.6 會變成 0.6。
下面這邊先標記為負數了,並取下一個字元

if(c=='-') 
   { 
      sign=-1; 
      c=m_stream.nextChar(); 
   } 

以-0.6為例的話,會進入上面if內,標記為負數,並取下一個字元0
然後繼續走到

if(c=='0') // 0
   {
      sign=1; // 這裡做sign=1會把負數變為正
      return true;
   }

進入if後 sign=1 又把它改回正數了,
然後退出parseInt這個方法,
走到parseNumber最下面的

value=sign*(intValue+fracValue)*MathPow(10,expValue);

-0.6 就變成 0.6了

@dingmaotu
Copy link
Owner

我看了一下代码,这个 parseInt 并不是独立的,是用在 parseNumber 中的,因此确实是有问题的。多谢~

@dingmaotu dingmaotu reopened this Sep 9, 2023
@dingmaotu dingmaotu self-assigned this Sep 9, 2023
@dingmaotu dingmaotu added the bug label Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants