help | c++ Question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

help | c++ Question

Post 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!
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: help | c++ Question

Post 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.
Last edited by short on Wed Mar 31, 2010 11:09 am, edited 3 times in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: help | c++ Question

Post 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!
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: help | c++ Question

Post 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 :)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: help | c++ Question

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: help | c++ Question

Post by short »

cant believe im giving up my 1337 for this...
I learned something from it :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: help | c++ Question

Post by Avishaiozeri »

I got it working! thank you all .
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: help | c++ Question

Post 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:
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply