help my math (int/double)

This is a discussion on help my math (int/double) within the C++ Programming forums, part of the General Programming Boards category; alright... i'm doing a battle system thing and I'm trying to revamp the whole battle class... here's a section of ...

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Unhappy help my math (int/double)

    alright... i'm doing a battle system thing and I'm trying to revamp the whole battle class... here's a section of the formula I want to check:
    Code:
    ...
    int atk
    ...
    int atkdam
    ...
    atkdam=p2.atk*static_cast<double>((1+rand()%100)/100.0);
    ...
    I was wondering if i got the casting right... I don't exactly remember the rules for float/int math... in this case, the atkdam is 1..100% of p2.atk... at least that's the way it should be working...

    short version: Is this code doing what I want and how much of it is garbage?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The cast is unnecessary, it's already forced by the division by 100.0

    However, a final explicit cast to int would probably avoid a warning about possible data loss.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 03:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 09:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21