c - 将 float 与整数相加时出现整数类型错误

标签 c type-mismatch msp430

我正在使用 C 在嵌入式设备上实现圆形绘制图形。我已经预先计算了 sin 和 cos 的三角查找表,并将它们存储为 const float 数组。每当我尝试将查找数组之一中的值添加到 newXnewY 变量时,我使用的 IDE(适用于 MSP430 的 Code Composer Studio)都会引发错误。 错误消息指出:“../main.c”,第 61 行:错误 #31:表达式必须具有整数类型,对应于此行:newX = (int) (newX + cosTable[i]);

完整代码:

#include <msp430.h>
#include "OLED.h"
#include "math.h"
#include "graphics.h"
void graphics_circle(int x, int y, int radius);
void main(void) {

    WDTCTL = WDTPW + WDTHOLD; // Stop WDT

    init();
    Delay_ms(1000);
    graphics_circle(32, 32, 16);

    while(1)
    {

    }


}

const float sinTable[] = {0.0, 0.018, 0.035, 0.053, 0.07, 0.088, 0.105, 0.122, 0.14, 0.157, 0.174, 0.191, 0.208, 0.225, 0.242, 0.259, 0.276, 0.293, 0.31, 0.326, 0.343, 0.359, 0.375, 0.391, 0.407, 0.423, 0.439, 0.454, 0.47, 0.485, 0.5, 0.516, 0.53, 0.545, 0.56, 0.574, 0.588, 0.602, 0.616, 0.63, 0.643, 0.657, 0.67, 0.682, 0.695, 0.708, 0.72, 0.732, 0.744, 0.755, 0.767, 0.778, 0.789, 0.799, 0.81, 0.82, 0.83, 0.839, 0.849, 0.858, 0.867, 0.875, 0.883, 0.892, 0.899, 0.907, 0.914, 0.921, 0.928, 0.934, 0.94, 0.946, 0.952, 0.957, 0.962, 0.966, 0.971, 0.975, 0.979, 0.982, 0.985, 0.988, 0.991, 0.993, 0.995, 0.997, 0.998, 0.999, 1.0, 1.0, 1.0, 1.0, 1.0, 0.999, 0.998, 0.997, 0.995, 0.993, 0.991, 0.988, 0.985, 0.982, 0.979, 0.975, 0.971, 0.966, 0.962, 0.957, 0.952, 0.946, 0.94, 0.934, 0.928, 0.921, 0.914, 0.907, 0.899, 0.892, 0.883, 0.875, 0.867, 0.858, 0.849, 0.839, 0.83, 0.82, 0.81, 0.799, 0.789, 0.778, 0.767, 0.755, 0.744, 0.732, 0.72, 0.708, 0.695, 0.682, 0.67, 0.657, 0.643, 0.63, 0.616, 0.602, 0.588, 0.574, 0.56, 0.545, 0.53, 0.516, 0.5, 0.485, 0.47, 0.454, 0.439, 0.423, 0.407, 0.391, 0.375, 0.359, 0.343, 0.326, 0.31, 0.293, 0.276, 0.259, 0.242, 0.225, 0.208, 0.191, 0.174, 0.157, 0.14, 0.122, 0.105, 0.088, 0.07, 0.053, 0.035, 0.018, 0.001, -0.017, -0.034, -0.052, -0.069, -0.087, -0.104, -0.121, -0.139, -0.156, -0.173, -0.19, -0.207, -0.224, -0.241, -0.258, -0.275, -0.292, -0.309, -0.325, -0.342, -0.358, -0.374, -0.39, -0.406, -0.422, -0.438, -0.453, -0.469, -0.484, -0.5, -0.515, -0.529, -0.544, -0.559, -0.573, -0.587, -0.601, -0.615, -0.629, -0.642, -0.656, -0.669, -0.681, -0.694, -0.707, -0.719, -0.731, -0.743, -0.754, -0.766, -0.777, -0.788, -0.798, -0.809, -0.819, -0.829, -0.838, -0.848, -0.857, -0.866, -0.874, -0.882, -0.891, -0.898, -0.906, -0.913, -0.92, -0.927, -0.933, -0.939, -0.945, -0.951, -0.956, -0.961, -0.965, -0.97, -0.974, -0.978, -0.981, -0.984, -0.987, -0.99, -0.992, -0.994, -0.996, -0.997, -0.998, -0.999, -0.999, -1.0, -0.999, -0.999, -0.998, -0.997, -0.996, -0.994, -0.992, -0.99, -0.987, -0.984, -0.981, -0.978, -0.974, -0.97, -0.965, -0.961, -0.956, -0.951, -0.945, -0.939, -0.933, -0.927, -0.92, -0.913, -0.906, -0.898, -0.891, -0.882, -0.874, -0.866, -0.857, -0.848, -0.838, -0.829, -0.819, -0.809, -0.798, -0.788, -0.777, -0.766, -0.754, -0.743, -0.731, -0.719, -0.707, -0.694, -0.681, -0.669, -0.656, -0.642, -0.629, -0.615, -0.601, -0.587, -0.573, -0.559, -0.544, -0.529, -0.515, -0.5, -0.484, -0.469, -0.453, -0.438, -0.422, -0.406, -0.39, -0.374, -0.358, -0.342, -0.325, -0.309, -0.292, -0.275, -0.258, -0.241, -0.224, -0.207, -0.19, -0.173, -0.156, -0.139, -0.121, -0.104, -0.087, -0.069, -0.052, -0.034, -0.017, -0.0, };
const float cosTable[] = {1.0, 1.0, 1.0, 0.999, 0.998, 0.997, 0.995, 0.993, 0.991, 0.988, 0.985, 0.982, 0.979, 0.975, 0.971, 0.966, 0.962, 0.957, 0.952, 0.946, 0.94, 0.934, 0.928, 0.921, 0.914, 0.907, 0.899, 0.892, 0.883, 0.875, 0.867, 0.858, 0.849, 0.839, 0.83, 0.82, 0.81, 0.799, 0.789, 0.778, 0.767, 0.755, 0.744, 0.732, 0.72, 0.708, 0.695, 0.682, 0.67, 0.657, 0.643, 0.63, 0.616, 0.602, 0.588, 0.574, 0.56, 0.545, 0.53, 0.516, 0.501, 0.485, 0.47, 0.454, 0.439, 0.423, 0.407, 0.391, 0.375, 0.359, 0.343, 0.326, 0.31, 0.293, 0.276, 0.259, 0.242, 0.225, 0.208, 0.191, 0.174, 0.157, 0.14, 0.122, 0.105, 0.088, 0.07, 0.053, 0.035, 0.018, 0.001, -0.017, -0.034, -0.052, -0.069, -0.087, -0.104, -0.121, -0.139, -0.156, -0.173, -0.19, -0.207, -0.224, -0.241, -0.258, -0.275, -0.292, -0.309, -0.325, -0.342, -0.358, -0.374, -0.39, -0.406, -0.422, -0.438, -0.453, -0.469, -0.484, -0.499, -0.515, -0.529, -0.544, -0.559, -0.573, -0.587, -0.601, -0.615, -0.629, -0.642, -0.656, -0.669, -0.681, -0.694, -0.707, -0.719, -0.731, -0.743, -0.754, -0.766, -0.777, -0.788, -0.798, -0.809, -0.819, -0.829, -0.838, -0.848, -0.857, -0.866, -0.874, -0.882, -0.891, -0.898, -0.906, -0.913, -0.92, -0.927, -0.933, -0.939, -0.945, -0.951, -0.956, -0.961, -0.965, -0.97, -0.974, -0.978, -0.981, -0.984, -0.987, -0.99, -0.992, -0.994, -0.996, -0.997, -0.998, -0.999, -0.999, -1.0, -0.999, -0.999, -0.998, -0.997, -0.996, -0.994, -0.992, -0.99, -0.987, -0.984, -0.981, -0.978, -0.974, -0.97, -0.965, -0.961, -0.956, -0.951, -0.945, -0.939, -0.933, -0.927, -0.92, -0.913, -0.906, -0.898, -0.891, -0.882, -0.874, -0.866, -0.857, -0.848, -0.838, -0.829, -0.819, -0.809, -0.798, -0.788, -0.777, -0.766, -0.754, -0.743, -0.731, -0.719, -0.707, -0.694, -0.681, -0.669, -0.656, -0.642, -0.629, -0.615, -0.601, -0.587, -0.573, -0.559, -0.544, -0.529, -0.515, -0.5, -0.484, -0.469, -0.453, -0.438, -0.422, -0.406, -0.39, -0.374, -0.358, -0.342, -0.325, -0.309, -0.292, -0.275, -0.258, -0.241, -0.224, -0.207, -0.19, -0.173, -0.156, -0.139, -0.121, -0.104, -0.087, -0.069, -0.052, -0.034, -0.017, -0.0, 0.018, 0.035, 0.053, 0.07, 0.088, 0.105, 0.122, 0.14, 0.157, 0.174, 0.191, 0.208, 0.225, 0.242, 0.259, 0.276, 0.293, 0.31, 0.326, 0.343, 0.359, 0.375, 0.391, 0.407, 0.423, 0.439, 0.454, 0.47, 0.485, 0.501, 0.516, 0.53, 0.545, 0.56, 0.574, 0.588, 0.602, 0.616, 0.63, 0.643, 0.657, 0.67, 0.682, 0.695, 0.708, 0.72, 0.732, 0.744, 0.755, 0.767, 0.778, 0.789, 0.799, 0.81, 0.82, 0.83, 0.839, 0.849, 0.858, 0.867, 0.875, 0.883, 0.892, 0.899, 0.907, 0.914, 0.921, 0.928, 0.934, 0.94, 0.946, 0.952, 0.957, 0.962, 0.966, 0.971, 0.975, 0.979, 0.982, 0.985, 0.988, 0.991, 0.993, 0.995, 0.997, 0.998, 0.999, 1.0, 1.0, 1.0, };

void graphics_init(){
    OLED_Init();
}
void graphics_circle(int x, int y, int radius){
    float i = 0;
    int newX = 0;
    int newY = 0;
    for(i = 0; i < 360; i+=1){
        newX = x;
        newX = (int) (newX + cosTable[i]);
        //newX = x + (int)cosTable[i];//*radius);
        //newY = y + (int)sinTable[i];//*radius);
        OLED_DrawPoint(newX, newY, 0);
    }
}

最佳答案

除了明显的算法问题之外,

您正在使用 float 作为数组索引。

使用整数值。比如说,int i = 0;

关于c - 将 float 与整数相加时出现整数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245905/

相关文章:

rust - 从 if 语句内部返回时类型不匹配

scala - 类型与 Spark-shell 中的相同类型不匹配

c - 带 msp430 的实时时钟

c - Unix 中的两个主题标签包含哪些文件类型?

c++ - Xcode 错误 : “EXC_BAD_ACCESS”

vba - 尝试激活工作表时出现类型不匹配 13 错误

c - 在 MSP430 软件中缩短 2 个不同的端口

timer - MSP 430G2553 使用 Timer_A 在比较模式下进行采样

c - 为什么在使用 fprintf 时不会打印到标准输出?

c - 为什么 GCC 填充这个位域?