Author Topic: Atmel studio warning that makes no sense  (Read 10484 times)

0 Members and 1 Guest are viewing this topic.

Online SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Atmel studio warning that makes no sense
« 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);
 

Offline daveshah

  • Supporter
  • ****
  • Posts: 356
  • Country: at
    • Projects
Re: Atmel studio warning that makes no sense
« Reply #1 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))
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Atmel studio warning that makes no sense
« Reply #2 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,
 

Online SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Atmel studio warning that makes no sense
« Reply #3 on: July 03, 2014, 10:19:40 am »
Oh shit, I now have 40 statements to correct  :-DD
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 294
  • Country: gb
Re: Atmel studio warning that makes no sense
« Reply #4 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))
« Last Edit: July 04, 2014, 11:32:54 am by gmb42 »
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: Atmel studio warning that makes no sense
« Reply #5 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?
 

Online SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Atmel studio warning that makes no sense
« Reply #6 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
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
Re: Atmel studio warning that makes no sense
« Reply #7 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).
 

Offline linux-works

  • Super Contributor
  • ***
  • Posts: 1997
  • Country: us
    • netstuff
Re: Atmel studio warning that makes no sense
« Reply #8 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.

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: Atmel studio warning that makes no sense
« Reply #9 on: July 03, 2014, 06:58:21 pm »
@linux-works: +1 and a hug
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: sk
Re: Atmel studio warning that makes no sense
« Reply #10 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 ;)
 

Offline Lukas

  • Frequent Contributor
  • **
  • Posts: 412
  • Country: de
    • carrotIndustries.net
Re: Atmel studio warning that makes no sense
« Reply #11 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...
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: sk
Re: Atmel studio warning that makes no sense
« Reply #12 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 ;)


 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: Atmel studio warning that makes no sense
« Reply #13 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.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
Re: Atmel studio warning that makes no sense
« Reply #14 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)

 

Offline vvanders

  • Regular Contributor
  • *
  • Posts: 124
Re: Atmel studio warning that makes no sense
« Reply #15 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.
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: sk
Re: Atmel studio warning that makes no sense
« Reply #16 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
 

Offline bwat

  • Frequent Contributor
  • **
  • Posts: 278
  • Country: se
    • My website
Re: Atmel studio warning that makes no sense
« Reply #17 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.
"Who said that you should improve programming skills only at the workplace? Is the workplace even suitable for cultural improvement of any kind?" - Christophe Thibaut

"People who are really serious about software should make their own hardware." - Alan Kay
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11622
  • Country: my
  • reassessing directives...
Re: Atmel studio warning that makes no sense
« Reply #18 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
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.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: sk
Re: Atmel studio warning that makes no sense
« Reply #19 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.
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 294
  • Country: gb
Re: Atmel studio warning that makes no sense
« Reply #20 on: July 04, 2014, 11:39:17 am »
with high level languages (python, perl, ruby... whatever) you simply can't do that.

ctypes??  Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: sk
Re: Atmel studio warning that makes no sense
« Reply #21 on: July 04, 2014, 01:38:04 pm »
with high level languages (python, perl, ruby... whatever) you simply can't do that.

ctypes??  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 ;)
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11622
  • Country: my
  • reassessing directives...
Re: Atmel studio warning that makes no sense
« Reply #22 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?
« Last Edit: July 04, 2014, 02:33:01 pm by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 294
  • Country: gb
Re: Atmel studio warning that makes no sense
« Reply #23 on: July 04, 2014, 02:46:13 pm »
with high level languages (python, perl, ruby... whatever) you simply can't do that.

ctypes??  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.
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: sk
Re: Atmel studio warning that makes no sense
« Reply #24 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 ?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf