Page 1 of 1

help | c++ Question

Posted: Wed Mar 31, 2010 10:51 am
by Avishaiozeri
I need help! I'm a beginner, and i was in the middle of writing a simple console combat simulator for practice.
Is there any way in c++ to check wich is the bigger from a list of numbers? i have an array of numbers, and i need to check wich of the numbers in the it is bigger. Please help! i'm stuck!

Re: help | c++ Question

Posted: Wed Mar 31, 2010 11:00 am
by short
Avishaiozeri wrote:I need help! I'm a beginner, and i was in the middle of writing a simple console combat simulator for practice.
Is there any way in c++ to check wich is the bigger from a list of numbers? i have an array of numbers, and i need to check wich of the numbers in the it is bigger. Please help! i'm stuck!
if you have just a regular array you could do something like this...

note: you need to know the size of the array ahead of time (you can almost always know this)...

Code: Select all

int position_Of_Largest_Number = 0;
int temp = 0;
for(int i = 0;i<arraySize;i++)
{
     if(myArray[i] > temp)
     {
          position_Of_Largest_Number= i;
          temp = myArray[i];
     }
}
This is definitely not the fastest way, but it will work!


edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.

Something like this:

Code: Select all

if( (float1 - float2) <= EPSILON)
{
     // they are the same number, do something

else
{
     // they are not the same number
with this method of checking if floating point numbers are the same you can then iterate through you array just like with integers and figure out which is the biggest number.

Re: help | c++ Question

Posted: Wed Mar 31, 2010 11:06 am
by Avishaiozeri
short wrote:
Avishaiozeri wrote:I need help! I'm a beginner, and i was in the middle of writing a simple console combat simulator for practice.
Is there any way in c++ to check wich is the bigger from a list of numbers? i have an array of numbers, and i need to check wich of the numbers in the it is bigger. Please help! i'm stuck!
if you have just a regular array you could do something like this...

note: you need to know the size of the array ahead of time (you can almost always know this)...

Code: Select all

int position_Of_Largest_Number = 0;
int temp = 0;
for(int i = 0;i<arraySize;i++)
{
     if(myArray[i] > temp)
     {
          position_Of_Largest_Number= i;
          temp = myArray[i];
     }
}
This is definitely not the fastest way, but it will work!
Thanks! i'll try it!

edited: At first i didn't understand it, but now i do. thanks, it works!

Re: help | c++ Question

Posted: Wed Mar 31, 2010 1:14 pm
by RyanPridgeon
short wrote: edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Actually your first method will work fine, because you're using "greater than", not checking equality.

Also kinda funny you picked 0.0000001, because that number cannot be stored exactly in floating point binary :)

Re: help | c++ Question

Posted: Wed Mar 31, 2010 1:55 pm
by avansc
RyanPridgeon wrote:
short wrote: edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Actually your first method will work fine, because you're using "greater than", not checking equality.

Also kinda funny you picked 0.0000001, because that number cannot be stored exactly in floating point binary :)
cant believe im giving up my 1337 for this...

the C standard library has a header called float.h, and in it there it a define, FLT_EPSILON or something like that. its the smallers number where a+b != a is false
i think its something like 1.192E-7 for single precision and 2.22E-16 for double precision. but machine epsilon is a real term for indicating this principal.

if you ever develop something that has to be IEEE compliant you HAVE to know this shit.

edit: http://en.wikipedia.org/wiki/Machine_epsilon

ps: that wiki i think is wrong, its not 2^-24 and -53, its -23 and -52, i cant find a IEEE document to validate, but my book "Numerical mathematics and computation"
http://www.amazon.com/Numerical-Mathema ... 0534389937
says it is.

Re: help | c++ Question

Posted: Wed Mar 31, 2010 2:06 pm
by short
cant believe im giving up my 1337 for this...
I learned something from it :)

Re: help | c++ Question

Posted: Wed Mar 31, 2010 2:34 pm
by Avishaiozeri
I got it working! thank you all .

Re: help | c++ Question

Posted: Wed Mar 31, 2010 5:20 pm
by RyanPridgeon
avansc wrote:
RyanPridgeon wrote:
short wrote: edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Actually your first method will work fine, because you're using "greater than", not checking equality.

Also kinda funny you picked 0.0000001, because that number cannot be stored exactly in floating point binary :)
cant believe im giving up my 1337 for this...

the C standard library has a header called float.h, and in it there it a define, FLT_EPSILON or something like that. its the smallers number where a+b != a is false
i think its something like 1.192E-7 for single precision and 2.22E-16 for double precision. but machine epsilon is a real term for indicating this principal.

if you ever develop something that has to be IEEE compliant you HAVE to know this shit.

edit: http://en.wikipedia.org/wiki/Machine_epsilon

ps: that wiki i think is wrong, its not 2^-24 and -53, its -23 and -52, i cant find a IEEE document to validate, but my book "Numerical mathematics and computation"
http://www.amazon.com/Numerical-Mathema ... 0534389937
says it is.
Was I right that you don't need it for the > comparison though?

Unless you do something like

Code: Select all

if (x > y + FLT_EPSILON) {
? But that seems kinda dodgey to me. Is it just for the equality operator or for < and > aswell?

Edit: sorry for kinda stealing the thread here D: