LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2013, 08:32 AM   #1
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Rep: Reputation: Disabled
Comparison Operators


I am using a switch statement to determine if a value is a Float or a string, then compairing it to another value that is of the same type. It will not do the comparison for some reason.

For example:

swith(fieldType){
case FLOAT: switch (compareOp){
case LESS: if (*((float*)(int offset) < (*((float *)char *valPtr))) return TRUE; else FALSE#: break;
case STRING: switch (compareOp){
case LESS: if (((char*)(int offset)) < (valPtr)) return TRUE; else FALSE#: break;


Is the problem in the <? or in the derefrencing? or is is something else?
 
Old 04-11-2013, 08:53 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What language is that? It looks a lot like C, but not enough like C to be correct.

Some specifics:

Code:
case FLOAT: switch (compareOp){
case LESS: if (*((float*)(int offset) < (*((float *)char *valPtr))) return TRUE;
 else FALSE#: break;
case STRING: switch (compareOp){
case LESS: if (((char*)(int offset)) < (valPtr)) return TRUE;
 else FALSE#: break;
What did you intend by the things I marked in red? Looks like you intended some kind of cast. But that is not the syntax for a cast.

Then the part I marked in purple, I don't even have a guess what it was supposed to mean.

Once you get the syntax correct, you also should know that comparing char pointers does not compare the strings. It compares the POINTERS.

C uses a char* like a string, but C doesn't really have a string type. You need various helper functions to do basic string operations, such as strcmp for comparing them.

You also seem to be assuming an int is the same size as a pointer. That is a bad idea. It will work in 32-bit x86, but will fail in many architectures including x86_64. You should learn correct use of void*

Last edited by johnsfine; 04-11-2013 at 09:05 AM.
 
Old 04-11-2013, 09:01 AM   #3
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
[QUOTE=vwinnecke;4929731]

swith(fieldType){
case FLOAT: switch (compareOp){
case LESS: if (*((float*)(int offset) < (*((float *)char *valPtr))) return TRUE; else FALSE#: break;
case STRING: switch (compareOp){
case LESS: if (((char*)(int offset)) < (valPtr)) return TRUE; else FALSE#: break;
QUOTE]

It is just sort of an algorithm. Did you see my questions at the bottom?

I will rewrite with some corrections:

For example:

switch(fieldType){
case FLOAT: switch (compareOp){
case LESS: if (*((float*)(int offset) < (*((float *)char *valPtr))) return TRUE; else FALSE; break;
case STRING: switch (compareOp){
case LESS: if (((char*)(int offset)) < (valPtr)) return TRUE; else FALSE; break;
 
Old 04-11-2013, 09:02 AM   #4
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
(int offset)

This part is defined as an integer, so it was

int offset;

then in the code offset. I just put int offset so I wouldn't have to define it earlier for this example.
 
Old 04-11-2013, 09:09 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by vwinnecke View Post
Did you see my questions at the bottom?
Yes. But no direct answer would be meaningful. You posted garbage code and asked too specific a question about which detail was wrong.

Quote:
I will rewrite with some corrections:

For example:
Not enough corrections.

Last edited by johnsfine; 04-11-2013 at 09:11 AM.
 
Old 04-11-2013, 09:13 AM   #6
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Maybe it would help if you knew this is comparing values in a database?
 
Old 04-11-2013, 09:18 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I think what you are trying to do is something like:

Code:
switch(fieldType){
  case FLOAT:
    switch (compareOp){
      case LESS:
        if ( *(float*)(offset) < *(float*)(valPtr) )
          return TRUE;
        else
          return FALSE; }
    break;
  case STRING:
    switch (compareOp){
      case LESS:
        if ( strcmp((char*)(offset),valPtr) < 0)
          return TRUE;
        else
          return FALSE; }
    break; }

Last edited by johnsfine; 04-11-2013 at 09:21 AM.
 
Old 04-11-2013, 09:54 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
If you're comparing values in a database, then I suggest that you use SQL queries to let the database-engine do the comparison. Let it return to you only the values that you are looking for.

Also, remember that floating-point values can't be meaningfully compared for 'equality.' You need to calculate the absolute-value of the difference between the two numbers and see if it is less than some small number. This can be expressed in SQL, e.g.:
(COL1 IS NOT NULL) AND (COL2 IS NOT NULL) AND (ABS(COL1 - COL2) < 0.0001)

Even if you see two numbers that are, say, "3.14" when printed, one might actually be "3.1415926" and the other might be "3.14159271" in their full, float-binary representation. (And in a database, they could also be decimal fields.)

You can use a ROUND() function to ask that the two values be rounded to an appropriate number of decimal places for subsequent comparison. These results might be directly comparable.

In the case of a database, remember NULL: the absence of any value at all. You can only compare numbers that exist.

Last edited by sundialsvcs; 04-11-2013 at 09:59 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Different Operators caponewgp Linux - Newbie 1 09-05-2009 08:54 PM
Bash syntax help: WHILE loop with multiple comparison operators dz-015 Linux - Software 4 04-21-2009 11:13 AM
Operators *^ and >> GodSendDeath Programming 4 11-01-2004 09:47 PM
C++, comparison operators, segfault exodist Programming 1 04-24-2004 03:03 PM
operators linuxanswer Programming 3 12-14-2003 06:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration