java - java中的 float 比较以识别模式

标签 java csv floating-point

我正在编写一个程序来识别股票市场数据中的模式,并且我正在尝试识别以下短期模式:

如果最低价比开盘价至少小 3 且收盘价在开盘价的 2 以内。

我正在读取以下格式的 CSV 文件中的值,但没有标题:

Open    High    Low     Close
353.4   359.2   347.7   349
351.4   354.08  349.1   353.1
350.1   354     349.3   350.2
352.4   353.28  348.7   349.8
345.7   352.3   345.7   351.5

这些值存储在名为 closePrice、openPrice、lowPrice 的 float 组列表中。我正在计算 这是我编写的代码,用于尝试识别数据中的模式。

    for(int i = 0; i < closePrice.size(); i ++)
    {
        //Difference between opening price and the price low
        float priceDrop = Math.abs(openPrice.get(i) - lowPrice.get(i));
        //Difference between opening price and close price (regardless of positive or negative)
        float closingDiff = Math.abs(openPrice.get(i) - closePrice.get(i));

        float dropTolerance = 3.0f;
        float closingTolerance = 2.0f;

        if( (priceDrop > dropTolerance) || (closingDiff < closingTolerance) )
        {
            System.out.println("price drop = " + priceDrop + " closing diff = " + closingDiff);
            System.out.println("Hangman pattern" + "\n");
        }
    }

所以它应该做的是测试价格是否下跌超过 3,然后收盘价是否在开盘价的 2 以内,但是当我运行程序时,它似乎让一切都绕过了 if 语句。我的输出是:

price drop = 5.6999817 closing diff = 4.399994
Hangman pattern
price drop = 2.2999878 closing diff = 1.7000122
Hangman pattern
price drop = 0.8000183 closing diff = 0.1000061
Hangman pattern

是不是因为我在比较花车?任何帮助将不胜感激。

最佳答案

看来您混淆了 AND 运算符和 OR 运算符。

你说你只想在两个条件都满足时输出,但你的代码说如果满足其中一个条件你就会输出。

关于java - java中的 float 比较以识别模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14733781/

相关文章:

java - 需要动态方法分派(dispatch)示例的帮助

Java:从小程序为 AES256 修补客户端安全策略

scala - 如何使用 Scala 解析带有空列的 CSV 数据?

r - 加载具有不同数量字符串的 csv 文件

java - 计算十进制值的双邻居

javascript - 处理大数时的中点 'rounding'?

java - 尝试使用 DOM 解析 XML 文件时出现 NullPointerException

java - 批处理文件重命名——从列表中插入文本(在 Python 或 Java 中)

python - 如何从 .csv 文件中提取数据并创建绘图?

Python奇怪的加法错误