Re: [PIC] Learning C
Isaac Marino Bavaresco <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.pic |
|---|---|
| Message-ID | <[email protected]> |
But you could use also the function 'isnan', which is easier to
understand for someone reading your code.
There are also other functions like 'isinf', etc.
Interestingly, a NaN is not greater, nor smaller nor equal to anything,
not even itself. It is only different than everything.
Cheers,
Isaac
Em 10/11/2024 20:09, smplx escreveu:
> Hi Colin,
>
> You might be intested to know that to check for floating point errors
> / exceptions in C you would use:
>
> if (x == x)...
> or
> if (x != x)...
>
> where x is a float
>
> e.g.
> float x
>
> x = 1.0 / 0.0;
>
> if (x != x)
> { // x is a NaN which is an error
> }
>
> Here clearly "x 'is the same as' x" but the test is "x 'is a NaN'".
>
> Friendly Regards
> Sergio
>
> On Sun, 10 Nov 2024, CDB wrote:
>
>> I have always separated the difference of = and == in C by referring
>> to the latter as 'is the same as'
>>
>> if (X is the same as Y) then ...... if (X == Y) then
>> x = y x now contains the value of Y.
>>
>> Colin
>>
>>
>> --
>>
>> [email protected] 11 November 2024
>>
>> Hosted by: www.superdomains.com.au
>>
>> This email is to be considered private if addressed to a named
>> individual or Personnel Department, and public if addressed to a
>> blog, forum or news article.
>>
>>
>>
>> __________________________________
>> Sent from eM Client | www.emclient.com <https://www.emclient.com/get>
>>
>>
>> On 11/11/2024 08:24:15, "smplx" <[email protected]> wrote:
>>
>>>
>>>
>>> On Sun, 10 Nov 2024, Neil wrote:
>>>
>>>> Agree with a couple things from other comments such as C being
>>>> high-level assembler and using a PC at first (you can just use gcc
>>>> already on Linux).
>>>>
>>>> Already knowing a structured language is very helpful.
>>>> But IMO you'll have to wrap your head around pointers, which can
>>>> get confusing. I find this C visualizer very helpful for figuring
>>>> out some algorithm... https://pythontutor.com/c.html
>>>>
>>>> One more note: = != ==
>>>> Or in other words, the equal sign ( = ) and the double equal size (
>>>> == ) are very different things in C. The first assigns a value and
>>>> the other compares values. But people new to C will invariably use
>>>> a single equal for a comparison and the code is true every time, as
>>>> C will compile it as valid code, do the assignment, then the
>>>> comparison. You have been warned ;P
>>>
>>> Sorry to wade in here but:
>>>
>>> if (y = x)...
>>>
>>> assigns x to y then compares the result to 0. 0 is nomally taken to
>>> mean false and "not 0" as true. So it is almost alway true except in
>>> the case where x is 0.
>>>
>>> I'm not sure what happens if x and y are floats and x is a NaN or if
>>> x and y are structures.
>>>
>>> if you're looking for a real headache try debugging a program that
>>> contains:
>>>
>>> if (! x == y)...
>>>
>>> Trying to learn C on bare metal is like trying to learn to fly by
>>> crashing a lot. It's a lot quicker and easier if you learn on a
>>> simulator.
>>>
>>> Friendly Regards
>>> Sergio Masci
>>>
>>> -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>>> View/change your membership options at
>>> https://mailman.mit.edu/mailman/listinfo/piclist
>> --
>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
>> View/change your membership options at
>> https://mailman.mit.edu/mailman/listinfo/piclist
>>
--
O software antivírus Avast realizou uma checagem de vírus neste e-mail.
www.avast.com
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
https://mailman.mit.edu/mailman/listinfo/piclist