0

I am stuck in finding checking whether a variable has value equal to an integer not. The given are the MALAB commands. The variable cc has value 1.0000 and I am checking whether its remainder is zero or not. it should be zero but it gives 1. Why doesn't this give 0?

>> cc
cc =
    1.0000
>> rem(cc,1)
ans =
    1.0000
>> rem(1.0000,1)
ans =
     0
>> mod(1.0000,1)
ans =
     0
>> mod(cc,1)
ans =
    1.0000
>> mod(1.0000,1)
mod(cc,1)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • ohhh thank you fprintf() returns 0.99999999999999989000000000000000 and 1==c return 0. – Mian Saeed Akbar Oct 11 '18 at 15:19
  • Now as it is very very near to 1, how can it convert it to 1? not by floor or ceil because I want to convert only those value to their nearest integer which are very very near to it, not all values. – Mian Saeed Akbar Oct 11 '18 at 15:21

1 Answers1

1

try fprintf("%.32f\n",cc);. cc is not exactly 1, as you think it is.

Try also 1==cc to see what that returns.

You can always round with your own criteria, as:

if abs(round(cc)-cc)<1e-8 % or some other number, you choose
    cc=round(cc);
end
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120