不可理解的模数计算

时间:2016-05-27 09:06:05

标签: r modulo

我的模数查询在这里得到了一个奇怪的结果。也许有人有解决方案:

/*
drop table student;
delimiter $$
CREATE TABLE `student` (
  `id` int(11) NOT NULL ,
  `name` char(1) NOT NULL,
  `email` varchar(1),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8$$

drop table email;
delimiter $$

CREATE TABLE `email` (
  `id` int(11) NOT NULL ,
  `type` varchar(8) NOT NULL,
  `person_id` int,
  `email` varchar(1),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8$$
;
*/
DELIMITER $
DROP PROCEDURE IF EXISTS CURSOR_DEMO$
CREATE PROCEDURE CURSOR_DEMO(start_student_id   INT
                            ,end_student_id     INT)
BEGIN

declare email_person_id int;
declare email_address   varchar(50);
DECLARE done    INT DEFAULT 0;
DECLARE CUR CURSOR FOR
    SELECT person_id,email FROM sandbox.email where type = 'student';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

    OPEN CUR;
        cur_loop:LOOP
                IF done = 1 THEN
                    LEAVE cur_loop;
                END IF;
                FETCH CUR INTO email_person_id,email_address;   
                update  student
                set     email = email_address
                where   id = email_person_id
                    and email is null
                ;
        END LOOP cur_loop;
    CLOSE CUR;
    commit;
END$
DELIMITER ;
truncate table student;
insert into student
values
(1,'A','1'),
(2,'B','1'),
(3,'C',null),
(4,'D',null),
(5,'E',null),
(6,'G',null),
(7,'F',null)
;
truncate table email;
insert into email
values
(1,'student',1,'a'),
(2,'student',2,'b'),
(3,'faculty',7,'z'),
(4,'student',3,'c')
;

select * from student;

call cursor_demo(1,10);

select * from student;

将给出结果:

d  <- seq(0.0,1.0,0.1)
lab.y <- ifelse(((d*10) %% 2.0 == 0.0),d, NA)

因此缺少 [1] 0.0 NA 0.2 NA 0.4 NA NA NA 0.8 NA 1.0

我尝试添加如下查询:

0.6

即使是

,也是ifelse((d*10/2 == 3.0), d, NA)
FALSE

... 我真的不明白这里发生了什么。

提前多多感谢!

1 个答案:

答案 0 :(得分:1)

这是由于浮点错误,你应该寻找低绝对差异而不是精确匹配。它不是0.6,而是(仅举例)0.6000000003或0.5999999997。尝试类似:

ifelse((abs((d*10) %% 2)<0.000001), d, NA)