Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 11-25-2006, 06:37 PM   #26 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

Quote:
Originally Posted by Sep102
Code:
hitsmax = (int)( ((1 / speed) * 3000) + 0.5 );
Casting it to int drops the fractional part of it completely, adding 0.5 to it before doing so rounds properly (i.e. 3.5 rounds to 4, 3.49 rounds to 3).
Casting to less precise numeric data type causes truncation, not rounding. That's why there is a Math.Round() function.
TheOutkastDev is offline   Reply With Quote
Old 11-25-2006, 07:08 PM   #27 (permalink)
Forum Expert
 
IHaveRegistered's Avatar
 
Join Date: Jun 2003
Location: Ontario
Age: 20
Posts: 4,520
Send a message via MSN to IHaveRegistered
Default

Quote:
Originally Posted by Sep102
Code:
hitsmax = (int)( ((1 / speed) * 3000) + 0.5 );
Casting it to int drops the fractional part of it completely, adding 0.5 to it before doing so rounds properly (i.e. 3.5 rounds to 4, 3.49 rounds to 3).
So, I should make it like...

t_hitsmax = ( ((1 / speed) * 3000) + 0.5 );
hitsmax = (int)t_hitsmax;

correct?
__________________
IHaveRegistered is offline   Reply With Quote
Old 11-25-2006, 07:12 PM   #28 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Yes, that will round the floating point numbers to whole numbers the way that you want them.
Sep102 is offline   Reply With Quote
Old 11-25-2006, 07:15 PM   #29 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by TheOutkastDev
Casting to less precise numeric data type causes truncation, not rounding. That's why there is a Math.Round() function.
That's why I add 0.5 to the value before I truncate it, it pushes x.y to (x + 1).z when y >= 0.5, thus truncating gives x + 1, and it pushes x.y to x.z when y < 0.5, thus truncating gives x.

And, yes, there is a Math.Round(), however most of his examples of code earlier were all C++, thus I gave an answer that would work in a managed or unmanaged environment.

Would you have preferred I wrote it as "round" instead of round?

Last edited by Sep102; 11-25-2006 at 07:21 PM.
Sep102 is offline   Reply With Quote
Old 11-25-2006, 07:45 PM   #30 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

Quote:
Originally Posted by Sep102
Would you have preferred I wrote it as "round" instead of round?
Yes. Simply because, when you go back to your code, you might forget why you added that 0.5 to your number. Whereas if you see your variable passed to a function "round" you know what the purpose is. Besides, writing a round function gives you something to add to your own personal library for other projects.
TheOutkastDev is offline   Reply With Quote
Old 11-25-2006, 07:50 PM   #31 (permalink)
Forum Expert
 
IHaveRegistered's Avatar
 
Join Date: Jun 2003
Location: Ontario
Age: 20
Posts: 4,520
Send a message via MSN to IHaveRegistered
Default

Quote:
Originally Posted by Sep102
That's why I add 0.5 to the value before I truncate it, it pushes x.y to (x + 1).z when y >= 0.5, thus truncating gives x + 1, and it pushes x.y to x.z when y < 0.5, thus truncating gives x.

And, yes, there is a Math.Round(), however most of his examples of code earlier were all C++, thus I gave an answer that would work in a managed or unmanaged environment.

Would you have preferred I wrote it as "round" instead of round?
Alright, i've tried the adding 0.5 idea and i've checked for a round function (Round, Math.Round, math.Round, and ditto with "round", not "Round"... all don't work (i was too lazy to check)). The adding 0.5 idea I believe I am doing wrong. Here's what I got going...

Code:
    int hitsmax = 0, hitsmin = 0, maxdamage = 0, mindamage = 0, weight = 0;
    double t_hitsmax = 0, t_hitsmin = 0, t_maxdamage = 0, t_mindamage = 0, t_weight = 0.0;

    t_hitsmax = (1 / wep[sub].speed) * 3000; //sub would be 0, and this is equal to 37
    t_hitsmin = t_hitsmax - 30;
    t_maxdamage = (wep[sub].strreq + wep[sub].speed + t_hitsmax) / 20; //strreq is 35, speed is 37
    t_mindamage = t_maxdamage - ((1 / wep[sub].speed) * 150); //speed is 37
    t_weight = (t_maxdamage + t_mindamage) / 2;

    hitsmax = (int)(t_hitsmax + 0.5);
    hitsmin = (int)(t_hitsmin + 0.5);
    maxdamage = (int)(t_maxdamage + 0.5);
    mindamage = (int)(t_mindamage + 0.5);
    weight = (int)(t_weight + 0.5);
Edit: Wait, I think I got it. I can floor it after I add the 0.5 XD
I think that's what you were saying. lmao *smacks head*
I'll go try this out.

Edit... Still didn't work. I'm guessing it may have to do with them being integers to say the least? (unless I was supposed to floor them in a different location? Like in the t_xxx...)

So, NOW I have this:

Code:
        t_hitsmax = (1 / wep[sub].speed) * 3000;
        t_hitsmin = t_hitsmax - 30;
        t_maxdamage = (wep[sub].strreq + wep[sub].speed + t_hitsmax) / 20;
        t_mindamage = t_maxdamage - ((1 / wep[sub].speed) * 150);
        t_weight = (t_maxdamage + t_mindamage) / 2;

        hitsmax = floor(t_hitsmax + 0.5);
        hitsmin = floor(t_hitsmin + 0.5);
        maxdamage = floor(t_maxdamage + 0.5);
        mindamage = floor(t_mindamage + 0.5);
        weight = (t_weight); //weight in UO is a double...
__________________

Last edited by IHaveRegistered; 11-25-2006 at 07:57 PM.
IHaveRegistered is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5