Re: [PIC] Learning C

smplx <[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.pic
Message-ID <[email protected]>
Hi Isaac,
The point isn't really what I could or should do, it's more to do with 
what someone else has written and the rest of us have to use. How often 
have you come across some code that was abandoned by the original author 
many years ago and has been hacked by uncareing individuals to fix an 
immediate problem. How often have you come across comments that no longer 
reflect what the code actually does.

And to make the NaN situation even more interesting a floating point 
library might implement NaNs correctly (or not) where the "noddy" 
MCU compiler doesn't. This is another reason why it's best to learn C in 
a proven high quality dev environment like a PC. Look at the early PIC C 
compilers - I dare you not to laugh.

Another way to express a NaN's character is to say "it is unordered".

Friendly Regards
Sergio Masci

On Sun, 10 Nov 2024, Isaac Marino Bavaresco wrote:

> 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
>
-- 
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
https://mailman.mit.edu/mailman/listinfo/piclist
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.