EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: Simon on July 03, 2014, 10:03:58 am

Title: Atmel studio warning that makes no sense
Post by: Simon on July 03, 2014, 10:03:58 am
I am getting:

Warning   4   comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]   

my code that produce this is:

Code: [Select]
if (714 < V < 723) //21.50-21.75
pwm_duty(1550);
Title: Re: Atmel studio warning that makes no sense
Post by: daveshah on July 03, 2014, 10:11:36 am
What your code is doing is
Code: [Select]
if ((714 < V) < 723)
What you want is
Code: [Select]
if ((V > 714) && (V < 723))
Title: Re: Atmel studio warning that makes no sense
Post by: Rerouter on July 03, 2014, 10:13:34 am
I agree, I've fallen into the same trap many times while learning my way around C,
Title: Re: Atmel studio warning that makes no sense
Post by: Simon on July 03, 2014, 10:19:40 am
Oh shit, I now have 40 statements to correct  :-DD
Title: Re: Atmel studio warning that makes no sense
Post by: gmb42 on July 03, 2014, 12:58:15 pm
Just for ease of visual review I usually write such things as

Code: [Select]
((LowVal < x) && (x < HiVal))
Title: Re: Atmel studio warning that makes no sense
Post by: andyturk on July 03, 2014, 02:45:57 pm
Oh shit, I now have 40 statements to correct  :-DD
You have 40 'if' statements (like that) in your code? Ouch. Aside from the syntax issue, is there a better way to accomplish what you're trying to do? E.g., a table of hi/lo values?
Title: Re: Atmel studio warning that makes no sense
Post by: Simon on July 03, 2014, 02:47:55 pm
I know it's probably not elegant or fast but I need to change a PWM based on supply voltage as well as an input selection so the code first checks which input is selected and then starts looking at the supply voltage in order to set the correct pwm
Title: Re: Atmel studio warning that makes no sense
Post by: zapta on July 03, 2014, 02:56:54 pm
Just for ease of visual reviewI usually write such things as

Code: [Select]
((LowVal < x) && (x < HiVal))

+1. I avoid relaying on operator precedence.

Another option, since the OP has 40 similar comparisons:

inline boolean isInRange(int v, int min, int max) {
  return (v > min) && v (v > max);
}

and then

if (isInRange(v, 714, 723)) {
}

This also semantically cleaner because v will be evaluated exactly once (e.g. in case it's a function call).
Title: Re: Atmel studio warning that makes no sense
Post by: linux-works on July 03, 2014, 02:59:55 pm
Just for ease of visual reviewI usually write such things as

Code: [Select]
((LowVal < x) && (x < HiVal))

yes!

pet peeve of mine: people do not use enough parens in if-statements.  I love to see explicit grouping.  rely on precedence?   no way, I don't always remember what comes before what, but with parens, its clear as day.

I encourage c programmers to use more parens like this.  its always better design to be very explicit, even if just to help readers understand what you wrote and meant.
Title: Re: Atmel studio warning that makes no sense
Post by: nctnico on July 03, 2014, 06:58:21 pm
@linux-works: +1 and a hug
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 03, 2014, 08:43:34 pm
Oh shit, I now have 40 statements to correct  :-DD

vim and regexps are your best friends to do that ;)
Title: Re: Atmel studio warning that makes no sense
Post by: Lukas on July 03, 2014, 10:13:25 pm
Luckily, there are languages where a<b<c means ((a<b) and (b<c)). Python is one of them. I'd have speculated that MATLAB would be equally smart (considering that MATLAB is for non-programmers) , but unfortunately, they're as dumb as C. MATLAB continues to disappoint me all the the time. Don't even begin with scoping...
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 03, 2014, 10:20:59 pm
they're as dumb as C.

C is a low-level language... it's mean't to be a language for writing operating systems and not math formulas ;)

have you ever noticed that more complex expressions are usually available only in HIGH-level languages ? it's that way for a reason (a very good reason) ;)

while using a low-level language the programmer is supposed to decompose the problem to a level needed by the language.  actually the very same apply for the high-level languages but you don't have to "dig" so deep with high-level languages ;)


Title: Re: Atmel studio warning that makes no sense
Post by: Bored@Work on July 04, 2014, 05:02:47 am
C is a low-level language... it's mean't to be a language for writing operating systems and not math formulas ;)

have you ever noticed that more complex expressions are usually available only in HIGH-level languages ? it's that way for a reason (a very good reason) ;)

while using a low-level language the programmer is supposed to decompose the problem to a level needed by the language.  actually the very same apply for the high-level languages but you don't have to "dig" so deep with high-level languages ;)

C is a high-level language. As good as any other. What you mean is a domain-specific language.

About that particular expression not understood by C. Every language has "formalities" you need to know. You can now argue that one formality is essential, or you can just learn what the language you use has, move on and spend your time solving the problem you really want to solve.

Since Python was mentioned. Many people argue Python's  "formalities", like the indentation style, are superior to other languages. While one formality (e.g. brackets) was just replaced by another (indentation). That is breaking continuity just for being different, not for adding anything of value. Same for interpreting (a < b < c) as ((a < b) and (b < c)). As a programmer you now have to learn that particular exception, and how the expression is evaluated, e.g. the precedence when it is used in combinations with others. That is not a "higher level", that is just annoying.
Title: Re: Atmel studio warning that makes no sense
Post by: zapta on July 04, 2014, 05:17:06 am
Same for interpreting (a < b < c) as ((a < b) and (b < c)). As a programmer you now have to learn that particular exception, and how the expression is evaluated, e.g. the precedence when it is used in combinations with others. That is not a "higher level", that is just annoying.

The two expressions have different semantic. In (a < b < c) range checking each of a, b, c, is evaluated once while in ((a < b) and (b < c))  b is evaluated twice. Examples:

(a < ++b < c)      vs     ((a < ++b) and (++b < c))
(a < readVoltage() < b)     vs     (a < readVoltage()) and (readVoltage() < c)

Title: Re: Atmel studio warning that makes no sense
Post by: vvanders on July 04, 2014, 05:27:54 am
C is a low-level language... it's mean't to be a language for writing operating systems and not math formulas ;)

have you ever noticed that more complex expressions are usually available only in HIGH-level languages ? it's that way for a reason (a very good reason) ;)

while using a low-level language the programmer is supposed to decompose the problem to a level needed by the language.  actually the very same apply for the high-level languages but you don't have to "dig" so deep with high-level languages ;)

C is a high-level language. As good as any other. What you mean is a domain-specific language.

About that particular expression not understood by C. Every language has "formalities" you need to know. You can now argue that one formality is essential, or you can just learn what the language you use has, move on and spend your time solving the problem you really want to solve.

Since Python was mentioned. Many people argue Python's  "formalities", like the indentation style, are superior to other languages. While one formality (e.g. brackets) was just replaced by another (indentation). That is breaking continuity just for being different, not for adding anything of value. Same for interpreting (a < b < c) as ((a < b) and (b < c)). As a programmer you now have to learn that particular exception, and how the expression is evaluated, e.g. the precedence when it is used in combinations with others. That is not a "higher level", that is just annoying.

I wouldn't say that python's whitespace restriction isn't adding anything. It forces a consistent coding style which is much appreciated when you have a lot of developersa in one codebase. I can't count the number of c/c++ styles I've seen over the years.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 04, 2014, 07:07:06 am

C is a high-level language. As good as any other. What you mean is a domain-specific language.


with C you can directly access memory - it's not high level for me.

link to wikipedia:
http://en.wikipedia.org/wiki/High-level_programming_language#Relative_meaning (http://en.wikipedia.org/wiki/High-level_programming_language#Relative_meaning)
Title: Re: Atmel studio warning that makes no sense
Post by: bwat on July 04, 2014, 07:33:21 am
with C you can directly access memory - it's not high level for me.
But you cannot directly query the status flags generated by the ALU or manipulate I/O ports so it is high-level for me (I say this as an implementor of Prolog compilers and runtimes so I know what high-level is, and I'm currently debugging microcode for a CISC CPU so I know what low-level is).  It's all relative and it's all meaningless. The real question is if it meets the spec. or not. Language wars are for the small minded.
Title: Re: Atmel studio warning that makes no sense
Post by: Mechatrommer on July 04, 2014, 07:34:06 am
C is a high-level language. As good as any other. What you mean is a domain-specific language.
with C you can directly access memory - it's not high level for me.
link to wikipedia:
http://en.wikipedia.org/wiki/High-level_programming_language#Relative_meaning (http://en.wikipedia.org/wiki/High-level_programming_language#Relative_meaning)
no wonder for modern C# or NET hash hash programmers to say such thing. low level language is machine code, and then there is assembly code which is one to one translation of the machine code, hence still a low level language. and then there are more human readable languages, it starts from language such as C/C++. if its a low level language, you wont have the luxury of doing nonsense things like "if (a < b < c) bracket" since its a nonsense in machine code domain or semantically ambiguous there is. and then there are more modern languages type to cope with OOP (creation) hassles?, they are managed languages, perharps thats what you are using right now. and no, you cant access memory directly in C (or any other type of languages, even assembly) in protected mode OS.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 04, 2014, 09:04:06 am
and no, you cant access memory directly in C (or any other type of languages, even assembly) in protected mode OS.

and what is a pointer then ? if not a pointer to a location in your address space ? no one says you can access the physical memory (that's hidden from you by the MM) - but you can freely poke around in the address space belonging to the process (virtual address space) with pointers in C.
with high level languages (python, perl, ruby... whatever) you simply can't do that.
Title: Re: Atmel studio warning that makes no sense
Post by: gmb42 on July 04, 2014, 11:39:17 am
with high level languages (python, perl, ruby... whatever) you simply can't do that.

ctypes (https://docs.python.org/2/library/ctypes.html)??  Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 04, 2014, 01:38:04 pm
with high level languages (python, perl, ruby... whatever) you simply can't do that.

ctypes (https://docs.python.org/2/library/ctypes.html)??  Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.

-- quote from ctypes web
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
-- end quote

does it sound to you like python itself can access the memory ? not to me ;)
Title: Re: Atmel studio warning that makes no sense
Post by: Mechatrommer on July 04, 2014, 02:20:09 pm
and what is a pointer then?
pointer is a "feature" provided by the language where it has no merit to decide if a laguange is low or high imho. from one perspective, "low or high" level was meant to how you interact programmatically with the native machine code. and from one perpective, pointers or arrays or even variables are all the same, they are addressing memory location. when you are assigning a = 2 you are actually manipulating memory space ;) granted with variables or arrays you cant control of where in the memory location you want to manipulate, do you think you can do that with pointers in protected mode? such as *ptr = whereeverilike ? you may try, there is reason once upon a time the blue screen was called "famous". exception to some hacks maybe, thats i call the deviation from an "ideal" or "theoritical" or "intended" implementation of the system. being able to hack from C doesnt necessarily means its low enough level. (2cnts from a person who lived the life)

and with pointers in C, you dont take it for granted, you need to call/pass system/OS's memory management or API to get the address. remember "free", "malloc", "new"? what do you think are they?
Title: Re: Atmel studio warning that makes no sense
Post by: gmb42 on July 04, 2014, 02:46:13 pm
with high level languages (python, perl, ruby... whatever) you simply can't do that.

ctypes (https://docs.python.org/2/library/ctypes.html)??  Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.

-- quote from ctypes web
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
-- end quote

does it sound to you like python itself can access the memory ? not to me ;)

As it can apparently pass memory into C functions that take a pointer and access same on the way out there is something going on there, not as simple as p = 0x1234; *p = 0x5678 I admit.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 04, 2014, 04:08:59 pm
and what is a pointer then?
pointer is a "feature" provided by the language where it has no merit to decide if a laguange is low or high imho. from one perspective, "low or high" level was meant to how you interact programmatically with the native machine code. and from one perpective, pointers or arrays or even variables are all the same, they are addressing memory location. when you are assigning a = 2 you are actually manipulating memory space ;) granted with variables or arrays you cant control of where in the memory location you want to manipulate, do you think you can do that with pointers in protected mode? such as *ptr = whereeverilike ? you may try, there is reason once upon a time the blue screen was called "famous". exception to some hacks maybe, thats i call the deviation from an "ideal" or "theoritical" or "intended" implementation of the system. being able to hack from C doesnt necessarily means its low enough level. (2cnts from a person who lived the life)

and with pointers in C, you dont take it for granted, you need to call/pass system/OS's memory management or API to get the address. remember "free", "malloc", "new"? what do you think are they?

blue screens, segfaults and kernel panics are exactly the consequence of possibility poking around in the address space belonging to the process... i'm not saying you can do whatever you want with pointers (actually you can , but with appropriate consequences)  - i'm saying it's possible with pointers in C and therefore C is low-level.

i used to write linux kernel modules (device drivers - for non linux guys) for custom hardware.... you don't have to explain me anything ;)

from my point of view (and it's not only me, there is lot of us over the world;) ) C is considered low level because you can directly manipulate the memory within the address space belonging to the process.

back in times when C was born it was considered high-level , but not now.... if C is high level, then  what would you call the real high level languages ? super-high-level ?
Title: Re: Atmel studio warning that makes no sense
Post by: zapta on July 04, 2014, 04:55:33 pm
from my point of view (and it's not only me, there is lot of us over the world;) ) C is considered low level because you can directly manipulate the memory within the address space belonging to the process.

That's a very narrow litmus test. By this logic every language peek/poke methods is low level regardless of all other aspects of the language.

back in times when C was born it was considered high-level , but not now.... if C is high level, then  what would you call the real high level languages ? super-high-level ?

That's a good point, high is a relative term, but you can take it to the other end, if C is low level then what is Assembly, a super low level? 

Personally I don't consider C to be 'low level' but 'system level', that is a high level language that is low enough for system level programming.
Title: Re: Atmel studio warning that makes no sense
Post by: miguelvp on July 04, 2014, 06:07:26 pm
I know people frown on self modifying code, but I once wrote a runtime assembler that will take a user's math formula and compiled it in memory and returned a pointer to the function so it could run faster than emulating the formula.
And yes, it was done in C using pointers.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 04, 2014, 06:29:03 pm

back in times when C was born it was considered high-level , but not now.... if C is high level, then  what would you call the real high level languages ? super-high-level ?

That's a good point, high is a relative term, but you can take it to the other end, if C is low level then what is Assembly, a super low level? 
actually the "gap" between assembly and C is mush narrower than the "gap" between C and today's high level languages.

Personally I don't consider C to be 'low level' but 'system level', that is a high level language that is low enough for system level programming.

OK. I buy this one ;)
Title: Re: Atmel studio warning that makes no sense
Post by: Mechatrommer on July 05, 2014, 01:58:36 am
if C is high level, then  what would you call the real high level languages ? super-high-level ?
no. i told you... Ms termed it as "managed" language ;) +1 to zapta. no one has redefined assembly or machine as "super low level" or anything thereof. but i agree, we are playing with relative term here... the lowest is machine, and next higher is assembly, the next higher is C, and then your super-high level language, or i prefer to call it "managed language" from where i stand.

and from my eye, anything, C, basic, delphi, or probably phyton (i'm not phytoner i dont know) are on the same level since they compile directly into machine native code. with only different intention and syntax or coding style. C is for performance and close relation to assembly or machine logic. whereas basic, delphi or phyton are only try to become human readable languages, but still compile directly to machine code. one step higher is the managed language, things that cannot run without external engine, virtual machine or whatnot, and/or heavily embedded with OOP classes or libraries in the programming environment.

but your judgement is based on memory manipulation capability, oh well, maybe you have more qualification in software engineering or computer science than i am hence i cant argue 100% ;)
http://hackaday.com/2011/03/17/writing-python-drivers-for-input-devices/ (http://hackaday.com/2011/03/17/writing-python-drivers-for-input-devices/)
http://stackoverflow.com/questions/981200/can-windows-drivers-be-written-in-python (http://stackoverflow.com/questions/981200/can-windows-drivers-be-written-in-python)
Title: Re: Atmel studio warning that makes no sense
Post by: Mechatrommer on July 05, 2014, 02:56:56 am
by your definition of pointer manipulation, the Visual Basic i'm using is not just partly managed language, but also system level language since i can do...
Code: [Select]
dim memPtr as long
dim myHackDataPtr as long
dim num as long

'setup myHackDataPtr and num
memPtr = whereeverilike
CopyMemory(memPtr, myHackDataPtr, num)
?
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 05, 2014, 08:34:09 pm
http://hackaday.com/2011/03/17/writing-python-drivers-for-input-devices/ (http://hackaday.com/2011/03/17/writing-python-drivers-for-input-devices/)
http://stackoverflow.com/questions/981200/can-windows-drivers-be-written-in-python (http://stackoverflow.com/questions/981200/can-windows-drivers-be-written-in-python)

actually you can write a "device driver" in any language - but only user space drivers. for such a driver you still need the low-level layer (written in the "native" language of the OS) which is acting as a bridge between kernel space and your user space driver. it has nothing to do with the capabilities or suitability of the particular language (you could write such a user space driver even in shell script :D)
Title: Re: Atmel studio warning that makes no sense
Post by: mikerj on July 06, 2014, 09:51:13 am
by your definition of pointer manipulation, the Visual Basic i'm using is not just partly managed language, but also system level language since i can do...
Code: [Select]
dim memPtr as long
dim myHackDataPtr as long
dim num as long

'setup myHackDataPtr and num
memPtr = whereeverilike
CopyMemory(memPtr, myHackDataPtr, num)
?

CopyMemory has nothing to do with Visual Basic, it's a Win32 API call (which is written in C).
Title: Re: Atmel studio warning that makes no sense
Post by: Mechatrommer on July 06, 2014, 11:02:12 am
CopyMemory has nothing to do with Visual Basic, it's a Win32 API call (which is written in C).
you bet any languages will need to bind to those APIs when allocating, modifying and freeing memory.
Title: Re: Atmel studio warning that makes no sense
Post by: Rigby on July 06, 2014, 01:19:53 pm
For me, the border between a high level language and low level language is pretty clear: high level languages don't require the developer to manage resources directly, and low level languages do.

Another way I've described it is that low level languages just do something, and high level languages either make you request permission to do something or make the request for you and hide it.

My definition is probably naïve.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 06, 2014, 01:35:20 pm
For me, the border between a high level language and low level language is pretty clear: high level languages don't require the developer to manage resources directly, and low level languages do.

agree ;)  ShinySuperClass.new() and then don't care... garbage collector will do whatever it needs to do once it's not referenced...  versus. malloc(sizeof struct) following by populating the pointers to functions...etc... and calling free() once your "object like feature" is not needed anymore :D
Title: Re: Atmel studio warning that makes no sense
Post by: andyturk on July 06, 2014, 04:13:59 pm
For me, the border between a high level language and low level language is pretty clear: high level languages don't require the developer to manage resources directly, and low level languages do.
[...]
My definition is probably naïve.
Yeah, probably.  :D

If the environment in which your C or C++ program runs uses garbage collection (http://en.wikipedia.org/wiki/Boehm_garbage_collector), then you don't have to explicitly free memory. The garbage collector will figure it out. You still have to allocate storage/objects yourself though.
Title: Re: Atmel studio warning that makes no sense
Post by: vvanders on July 06, 2014, 04:36:04 pm
That's just an allocation library, its still not a native feature of the language. There's also things like c++/CLI that can have GC semantics which are built into the language.
Title: Re: Atmel studio warning that makes no sense
Post by: zapta on July 06, 2014, 04:58:55 pm
Wikipedia has an entry on the topic http://en.wikipedia.org/wiki/High-level_programming_language (http://en.wikipedia.org/wiki/High-level_programming_language) . My reading is that high 'levelness' is a relative term and one language can be higher or lower than other.
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 06, 2014, 06:44:17 pm
Wikipedia has an entry on the topic http://en.wikipedia.org/wiki/High-level_programming_language (http://en.wikipedia.org/wiki/High-level_programming_language) . My reading is that high 'levelness' is a relative term and one language can be higher or lower than other.

== quote from that wikipedia article
Today, many programmers might refer to C as low-level, as it lacks a large runtime-system (no garbage collection, etc.), basically supports only scalar operations, and provides direct memory addressing. It, therefore, readily blends with assembly language and the machine level of CPUs and microcontrollers.
== end quote

"provides direct memory addressing" , "many programmers might refer to C as low-level" .... weren't you Guys fighting me because of such a statements ? :D (no offense ;))  we already agreed on a term "system level" language , so no further fight  ;)
Title: Re: Atmel studio warning that makes no sense
Post by: zapta on July 06, 2014, 06:54:57 pm
== quote from that wikipedia article
Today, many programmers might refer to C as low-level, as it lacks a large runtime-system (no garbage collection, etc.), basically supports only scalar operations, and provides direct memory addressing. It, therefore, readily blends with assembly language and the machine level of CPUs and microcontrollers.
== end quote

It also goes in the other direction ;-)

"Assembly language may itself be regarded as a higher level (but often still one-to-one if used without macros) representation of machine code, as it supports concepts such as constants and (limited) expressions, sometimes even variables, procedures, and data structures."

BTW, for the people that consider C to be low level, do you consider C++ to be low level as well?
Title: Re: Atmel studio warning that makes no sense
Post by: rob77 on July 06, 2014, 07:08:34 pm
== quote from that wikipedia article
Today, many programmers might refer to C as low-level, as it lacks a large runtime-system (no garbage collection, etc.), basically supports only scalar operations, and provides direct memory addressing. It, therefore, readily blends with assembly language and the machine level of CPUs and microcontrollers.
== end quote

It also goes in the other direction ;-)

"Assembly language may itself be regarded as a higher level (but often still one-to-one if used without macros) representation of machine code, as it supports concepts such as constants and (limited) expressions, sometimes even variables, procedures, and data structures."

BTW, for the people that consider C to be low level, do you consider C++ to be low level as well?

not at all - i would consider C++ as blend of low and high-level (still got the "low" level features of C , but provides "high" level features at the same time). so if we use the term "system level  language", then C++ would be there along with C but on a "higher" level than C ;)