Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Floats and Ints

Author
Message
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 28th Jul 2008 10:09
I'm working on a battle system for a turn-based RPG. Currently, it involves somewhat simple formulae: only int's and char's. I want to include floats for small modifications (mostly random between 0.5 and 0.7, for example). My question is this: if I mix float's and int's without any explicit casting, will my results be the expected values? For example (note this is not at all the formula I'm using):



Am I correct in assuming that the random float will affect the outcome of the formula as I intend? Bascially, is this an unreliable method?

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 28th Jul 2008 12:03
The first question one must ask?
You have a formula like this.

A/B*C

are you wanting it to be
(A/B)*C

Or are you wanting it to be
A/(B*C)
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 28th Jul 2008 12:05
at the moment it is using BIDMAs so it will do the division first
sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 28th Jul 2008 12:18
Quote: "moment it is using BIDMAs"


I was taught it to be "BODMAS"

The thing to remember is that division and multiplication have the same level, and addition with subtraction have the same level.

If you have:
A*B/C

You can not assume it will be operated as
A*(B/C) as is implied in the "BODMAS" rule.
It will in all probability be calculated as
(A*B)/C

I am just trying to see what the OP is actually trying to do before giving an answer.
sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 28th Jul 2008 12:28
Maybe it is better to answer the question in a generic form:

If one operand is a long double
THEN others are all converted to long doubles
ELSE IF one operand is a double
THEN others are all converted to doubles
ELSE IF one operand is a float
THEN others are all converted to float
ELSE IF one operand is a unsigned long
THEN others are all converted to unsigned long
ELSE IF one operand is a long
THEN others are all converted to long
ELSE IF one operand is a unsigned
THEN others are all converted to unsigned


Just remember that whenever a char or a short int is used in an expression, its value is automatically elevated to an int during the evaluation of the expression, but its value will stay as it was for its own variable.
IE: a copy of the value will actually be elevated to an int.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 28th Jul 2008 20:17
C++ standard precedence is used.

So, will a formula similar to my original work as planned? Or will there be some strange inaccuracies that will negatively affect the outcome? (I don't mean minimal, of course. I mean enough to matter.)

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 28th Jul 2008 21:04
why dont you try it? do something like
int num = 10*0.7;
and see what it gives you
if it works it gives ou 7, if not it will give you either 10 or 0, 0 i think because it always rounds down (?)
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 28th Jul 2008 21:17


This cout's 6.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Jul 2008 23:50
It's standard float-point variation.



Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 29th Jul 2008 00:04
So, in my attack formula, will it do alright? Or will it cause trouble?

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 29th Jul 2008 00:10
I think I'm going to start using doubles. They correctly output 7.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo

Login to post a reply

Server time is: 2024-11-20 17:46:42
Your offset time is: 2024-11-20 17:46:42