【问题标题】:Illegal instruction on trying to perform 1 - (a double number)尝试执行 1 的非法指令 - (双数)
【发布时间】:2014-06-18 09:39:00
【问题描述】:

我有一个名为“a”的数据类型为 double 的数字,当这个数字小于 0.5 时说 b = 1 - a 没有错误。 但是当 a > 0.5 时,我说 b = 1 - a 代码中的那个点有一个错误(执行此特定行之前的所有打印语句)并且错误显示“非法指令” 这里发生了什么?

代码是:

enter code here
void pwm_duty(double duty)
{
int count;
int on_time;
int off_time;
double on;
double off; 
on = duty * 10; 
off = (1-duty) * 10;
printf("on %f off %f\n",on,off);
on_time = floor(on);    
off_time = floor(off);
printf("Before loop on time is %d off time is %d \n",on_time, off_time);
for(count=0;count<20;count++)
{
    ioctl(fd,1,1);
    delay_ms(on_time);
    ioctl(fd,0,1);
    delay_ms(off_time);
}
printf("Now I am out of the loop lol\n");
}  

错误是: 非法教学

【问题讨论】:

  • 你能显示代码吗?我们需要更多上下文
  • 并完成错误信息。
  • 如果我猜测(这是我所能做的,因为 NSA 已经解决了这个问题),你的程序逻辑会导致一个 indeterminate double 值被评估.即,您正在为从未 设置 为有效浮点数的浮动指针操作回避 var。这就是我的全部。
  • 通常这是由于损坏或数字越界造成的。一些代码会有所帮助,因为您看到的是由其他原因引起的副作用。
  • 我已将代码添加到我的问题中。这将在 FriendlyARM 板上执行,因此该功能是更大代码的一部分。占空比将作为主代码的一部分输入,并使用该数字(这是一个浮点值)调用函数 pwm_duty

标签: c type-conversion


【解决方案1】:

你肯定错过了一些东西。我不知道你是如何完成这个任务的,但是完成这个减法(并打印结果)的正确方法是:

#include <stdio.h>

   void main()

 {
       double a=0.4;
       double b=0.4;
       double c=0.6;


  b = 1 - a;

  printf("b is %f\n",b);

  b = 1 - c;


  printf("b is %f\n",b);

  return;


 }

而这个程序对应的输出是

$ ./a.out
b is 0.600000
b is 0.400000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多