The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > The ProgrammersTalk > General Talk


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #11 (permalink)  
Old 07-10-2007, 01:31 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Quote:
Originally Posted by siLenTz View Post
Using binary also a great solution and I don't thing it require alot of
math at all: Here for field that allow more than one option selected:


This mean we selected Red and White. Using Binary is not consider
an advanced math right?

You have the idea, but go up to your grandfather (assuming no math background there) and show him that image and ask him if he gets it. I know that my entire family would get glassy eyed, laugh in my face, and remind me that math was a long time ago for them.

I'm writing up a mini-prog to demonstrate what I'm talking about. Will post soon.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #12 (permalink)  
Old 07-10-2007, 01:36 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
I'm going to give this code in 2 posts. In this post, the form would store the users' preference.
PHP Code:
<?php
//The "overhead" of assigning a power of 2 to each of the respective values.
define ('GENDER_MALE',1);
define ('GENDER_FEMALE',2);
define ('GENDER_TRANSGENDER',4);

define ('HAIR_RED',1);
define ('HAIR_BLOND',2);
define ('HAIR_BLACK',4);
define ('HAIR_BROWN',8);

define ('EYE_BROWN',1);
define ('EYE_HAZEL',2);
define ('EYE_AMBER',4);
define ('EYE_GREEN',8);
define ('EYE_BLUE',16);
define ('EYE_GRAY',32);
define ('EYE_VIOLET',64);

if (isset(
$_POST)) { //Weak assumption, but this just a proof-of-concept code
  //data posted, so save
  
$entered_gender = (int)$_POST['gender']; //Type casting for data validation.
  
$entered_hair  = (int)$_POST['hair'];
  
$entered_eye array_sum($_POST['eye']);
  
  
//Assume user id in a session variable which was set during login.
  
mysql_query("update profile set gender='.$entered_gender.',hair='.$entered_hair.',eye=".$entered_eye." where user_id=".$_SESSION['user_id']);
  
}

echo 
'<form method="post">
<h2>Gender</h2>
Male <input type="radio" name="gender" value="'
.GENDER_MALE.'" /><br />
Female <input type="radio" name="gender" value="'
.GENDER_FEMALE.'" /><br />
Transgender <input type="radio" name="gender" value="'
.GENDER_TRANSGENDER.'" /><br />

<h2>Hair Color</h2>
Red <input type="radio" name="hair" value="'
.HAIR_RED.'" /><br />
Blond <input type="radio" name="hair" value="'
.HAIR_BLOND.'" /><br />
Black <input type="radio" name="hair" value="'
.HAIR_BLACK.'" /><br />
Brown <input type="radio" name="hair" value="'
.HAIR_BROWN.'" /><br />

<h2>Eye Color</h2>
(Check all that apply)
Brown <input type="checkbox" name="eye[]" value="'
.EYE_BROWN.'" /><br />
Hazel <input type="checkbox" name="eye[]" value="'
.EYE_HAZEL.'" /><br />
Amber <input type="checkbox" name="eye[]" value="'
.EYE_AMBER.'" /><br />
Green <input type="checkbox" name="eye[]" value="'
.EYE_GREEN.'" /><br />
Blue <input type="checkbox" name="eye[]" value="'
.EYE_BLUE.'" /><br />
Gray <input type="checkbox" name="eye[]" value="'
.EYE_GRAY.'" /><br />
Violet <input type="checkbox" name="eye[]" value="'
.EYE_VIOLET.'" /><br />
<input type="submit" value="Save Profile" />
</form>
?>

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #13 (permalink)  
Old 07-10-2007, 01:43 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
This would be the search form. This code isn't tested, just composed on the fly, but it should give a sufficient idea of what I was discussing.

PHP Code:
<?php
//The "overhead" of assigning a power of 2 to each of the respective values.
define ('GENDER_MALE',1);
define ('GENDER_FEMALE',2);
define ('GENDER_TRANSGENDER',4);

define ('HAIR_RED',1);
define ('HAIR_BLOND',2);
define ('HAIR_BLACK',4);
define ('HAIR_BROWN',8);

define ('EYE_BROWN',1);
define ('EYE_HAZEL',2);
define ('EYE_AMBER',4);
define ('EYE_GREEN',8);
define ('EYE_BLUE',16);
define ('EYE_GRAY',32);
define ('EYE_VIOLET',64);

if (isset(
$_POST)) { //Weak assumption, but this just a proof-of-concept code
  //Query sent, so process
  
$entered_gender array_sum($_POST['gender']); //Type casting for data validation.
  
$entered_hair  array_sum($_POST['hair']);
  
$entered_eye array_sum($_POST['eye']);
  
  
$where_clause ''$prefix '';
  if (
$entered_gender 0) {
    
$where_clause '(gender & '.$entered_gender.')';
    
$prefix ' & ';
  }
  if (
$entered_hair 0) {
    
$where_clause $prefix.'(hair & '.$entered_hair.')';
    
$prefix ' & ';
  }
  if (
$entered_eye 0) {
    
$where_clause $prefix.'(eye & '.$entered_eye.')';
  }
  if (
strlen($where_clause) > 0) {
    
$where_clause "where ".$where_clause;
  }
  
  
//Assume user id in a session variable which was set during login.
  
mysql_query("select * from  profile ".$where_clause);
  
}

echo 
'<form method="post">
Search for Matching Profiles
<h2>Gender</h2>
Male <input type="checkbox" name="gender" value="'
.GENDER_MALE.'" /><br />
Female <input type="checkbox" name="gender" value="'
.GENDER_FEMALE.'" /><br />
Transgender <input type="checkbox" name="gender" value="'
.GENDER_TRANSGENDER.'" /><br />

<h2>Hair Color</h2>
Red <input type="checkbox" name="hair" value="'
.HAIR_RED.'" /><br />
Blond <input type="checkbox" name="hair" value="'
.HAIR_BLOND.'" /><br />
Black <input type="checkbox" name="hair" value="'
.HAIR_BLACK.'" /><br />
Brown <input type="checkbox" name="hair" value="'
.HAIR_BROWN.'" /><br />

<h2>Eye Color</h2>
Brown <input type="checkbox" name="eye[]" value="'
.EYE_BROWN.'" /><br />
Hazel <input type="checkbox" name="eye[]" value="'
.EYE_HAZEL.'" /><br />
Amber <input type="checkbox" name="eye[]" value="'
.EYE_AMBER.'" /><br />
Green <input type="checkbox" name="eye[]" value="'
.EYE_GREEN.'" /><br />
Blue <input type="checkbox" name="eye[]" value="'
.EYE_BLUE.'" /><br />
Gray <input type="checkbox" name="eye[]" value="'
.EYE_GRAY.'" /><br />
Violet <input type="checkbox" name="eye[]" value="'
.EYE_VIOLET.'" /><br />
<input type="submit" value="Save Profile" />
</form>
?>
OK, that posted, I just want to say that while it doesn't require a lot of math, it does require such a background to come up with and fully understand which was my whole point: knowledge of mathematics can be very helpful even though routines may be written which avoid its usage.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #14 (permalink)  
Old 07-10-2007, 02:01 AM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
I agree, it is a very great solution. However, It still have there own
disadvantage. For example: I am run my own clothes shop. My shop sell
many shirt with many difference colour. There are Red, Yellow, Black, White
and Blue. Now my customers check my website and search for shirt which
is can be Red, Yellow, Black, and it can be mix with those three colour.
So the possible outcome is: R,Y,B,RY,RB,YB,RYB. In this case you using binary
isn't so effective. Imagine if there are over 20 of difference options can
be select more than one and you want to search with keyword OR instead
of AND and it is going to be insane.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #15 (permalink)  
Old 07-10-2007, 02:08 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Quote:
Originally Posted by siLenTz View Post
I agree, it is a very great solution. However, It still have there own
disadvantage. For example: I am run my own clothes shop. My shop sell
many shirt with many difference colour. There are Red, Yellow, Black, White
and Blue. Now my customers check my website and search for shirt which
is can be Red, Yellow, Black, and it can be mix with those three colour.
So the possible outcome is: R,Y,B,RY,RB,YB,RYB. In this case you using binary
isn't so effective. Imagine if there are over 20 of difference options can
be select more than one and you want to search with keyword OR instead
of AND and it is going to be insane.
Well, you can change the binary operator and still have success. It's 1 a.m. now though and my brain is shutting down, so let me pick this up tomorrow. I'll start another thread, however, so that we stay on topic.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #16 (permalink)  
Old 07-10-2007, 02:10 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by siLenTz View Post
Using binary also a great solution and I don't thing it require alot of
math at all: Here for field that allow more than one option selected:


This mean we selected Red and White. Using Binary is not consider
an advanced math right? If we selected Black, Red and Whitr color
then the binary should be 1 1 0 0 1 and we can generate it into\
interger by simply: (1)(2^0)+(0)(1^2)+(0)(2^2)+(1)(2^3)+(0)(2^4)
Another way to use binary is combinations of 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 and of course you can apply it to larger values. It can be used to set a mask for something like colors (something that was actually done in the now ancient programming application QBasic). In QBasic (and the Win32 API, I believe), the following basic colors are defined:
1 = Blue
2 = Green
4 = Red
8 = Brightness

Using that alone, you can have a 16-color palette:
0 = Black (0|0|0|0)
1 = Blue (0|0|0|1)
2 = Green (0|0|1|0)
3 = Cyan (0|0|1|1)
4 = Red (0|1|0|0)
5 = Magenta/Violet/Purple (0|1|0|1)
6 = Yellow* (0|1|1|0)
7 = White (0|1|1|1)
8 = Grey (1|0|0|0)
9 = Bright blue (1|0|0|1)
10 = Bright green (1|0|1|0)
11 = Bright cyan (1|0|1|1)
12 = Bright red (1|1|0|0)
13 = Bright magenta (1|1|0|1)
14 = Bright yellow (1|1|1|0)
15 = Bright white (1|1|1|1)
* - In QBasic, color 6 was actually brown, which was a "hacked" color because there is a pattern to the values when matched with their corresponding RGB values of HTML, CSS, Photoshop, etc.

Notice how it was all 1s and 0s? Think of it this way: 0 = off, 1 = on. If the number at the spot is 1, that color is turned on. If it is 0, it is turned off. Also note the following:
(8|4|2|1)
Remember the basic colors discussed earlier? The 1s and 0s simply mean:
0 * brightness | 1 * red | 1 * green | 0 * blue = 0*8 | 1*4 | 1*2 | 0*1, which is 6 because 0 + 4 + 2 + 0 = 6 (yellow). This means that yellow is a mixture of red and green.

Back to the mask idea, suppose you wanted to fetch the BrRGB values individually for some sort of object or structure. All you would need to use is the equivalent statement in your programming language:

Code:
<** And in this case refers to a bitwise AND, commonly represented in C/C++, Java and PHP, as well as other programming languages, by the ampersand (&) **>
color = 9  <** 9 is bright blue, remember? **>
bright = (color And 8)
red = (color And 4)
green = (color And 2)
blue = (color And 1)
In that case, 9 And 8 = 8, 9 And 4 = 0, 9 And 2 = 0, 9 And 1 = 1. This gives you 8 + 0 + 0 + 1 = 9. A common way to add the values of 0x8, 0x4, 0x2, 0x1, etc. to possibly get the full value of the color rather than a separated value is by using a bitwise OR (represented using the pipe ( | ) symbol in many programming languages). It is faster than normal addition too. Here is what it would look like:
color = bright Or red Or green Or blue

To understand why that works would require more in-depth knowledge of binary and bitwise operations, something that is beyond the scope of this post. For more information, check out one or more of the following sites:

PHP Bitwise Tutorial by Jim Plush - Talks about binary and bitwise operations in PHP; note that variables in PHP are things like "$a" or "$hello" rather than just "a" or "hello".

GameDev.net - Bitwise Operations in C - Talks about bitwise operators in C, various uses as applied to game development, etc.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #17 (permalink)  
Old 07-10-2007, 02:18 AM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
I think we went too far from the topic. I didn't disagree that math isn't
important in application. However, It just some application is use it more
than some other. Basically, some application doesn't require advanced math.
I remember maybe 6 years old, I made a Syntax Editor(which heighlight your
code into readable colour) and I didn't use any advanced math(I did use
some math operators but it is what we have learn since we were a kid).
Futhermore, most of high-level programming langauge nowaday make
it more easier for people who have poor math background. In conclusion,
every application need math but it is just matter of less or more.
Last but not least, even those you have great math background but
without technique or experience of programming, it still difficult to solve some
problem.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #18 (permalink)  
Old 07-10-2007, 02:27 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
ROFLMFAO, rpgfan3233. You're going to give a non-math nerd that reads that a heart attack! Having been programming for awhile, I found it interesting that you cited QBasic. Do you know if you assertions hold for gw-basic and Apple IIE BASIC? Time to peek -16384 and seriously frighten the mathless away.

Quote:
Originally Posted by siLenTz
I think we went too far from the topic.
Agreed. I was a bit nervous initially posting an algo detail b/c we might do that. This conversation on using binary would be great to continue in another thread.
Quote:
Originally Posted by siLenTz
even those you have great math background but without technique or experience of programming, it still difficult to solve some problem.
100% agree. Many math nerds just can't cut it when it comes to programming. It's a bit funny to watch them try to get through Numerical Analysis or Numerical Linear Algebra - they become as frightened of programming as some programmers are of math.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #19 (permalink)  
Old 07-10-2007, 02:34 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
I've split off the binary and options discussion to its own thread at Options & Binary Optimization .

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #20 (permalink)  
Old 08-21-2007, 12:37 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 441
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Quote:
Originally Posted by TeraTask View Post
Mathematical logic, in fact, is a fundamental necessity in my not-so-humble opinion. For instance, w/o such logic, could you easily determine if the following 2 conditional expressions are equivalent?

Code:
if (!($user_logged_in && $user_attempting_to_login)) {
//Do something
}
Code:
if (!$user_logged_in || !$user_attempting_to_login) {
}
Personally, I find the second easier to read.

For those who aren't sure, those 2 statements are logically equivalent. Namely, NOT(A and B) => NOT(A) or NOT(B)

I thought it interesting that just yesterday I needed this mathematical logic to resolve a case where I had a statement isomorphic to this:
Code:
if (A == a && (B != b || C != c)) {
//Do something
}
Then, later on (so not appropriate for an "else" clause above) I needed to test for the opposite condition:
Code:
if (A != a || (B == b && C == c)) {
//Do something else
}
I have seen people who, not having a strong background in math, would write

Code:
if (!(A == a && (B != b || C != c))) {
//Do something
}
where "!" means "not". Which of those has the higher number of calculations required across all possible values of A, B, C, a, b, and c?

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply

&la