Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Arithmetic Problem

Arithmetic Problem

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CNewbie
    wrote on last edited by
    #1

    When I try to perform the math below I get an answer of 0, but the result of the below should be 5.76 Int64 lpTotalNumberOfFreeBytes = 41085476864; Int64 lpTotalNumberOfBytes = 712212955136; Int64 strAnswer = ((lpTotalNumberOfFreeBytes / lpTotalNumberOfBytes) * 100); Does anyone see what I am doing wrong here? Thanks

    L M 2 Replies Last reply
    0
    • C CNewbie

      When I try to perform the math below I get an answer of 0, but the result of the below should be 5.76 Int64 lpTotalNumberOfFreeBytes = 41085476864; Int64 lpTotalNumberOfBytes = 712212955136; Int64 strAnswer = ((lpTotalNumberOfFreeBytes / lpTotalNumberOfBytes) * 100); Does anyone see what I am doing wrong here? Thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      hi, int/int performs an integer division; you should consider one of these fixes: - use float or double everywhere - postpone division, hence do 100*int/int instead of int/int*100 (there is an increased risk of overflowing when doing all multiplies before all divides) The above holds true in most programming languages (C, C++, Java, C#, ...). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      1 Reply Last reply
      0
      • C CNewbie

        When I try to perform the math below I get an answer of 0, but the result of the below should be 5.76 Int64 lpTotalNumberOfFreeBytes = 41085476864; Int64 lpTotalNumberOfBytes = 712212955136; Int64 strAnswer = ((lpTotalNumberOfFreeBytes / lpTotalNumberOfBytes) * 100); Does anyone see what I am doing wrong here? Thanks

        M Offline
        M Offline
        mmikey7
        wrote on last edited by
        #3

        You can use float everywhere as Luc adviced you. But if you for some reason need to store lpTotalNumberOfFreeBytes and lpTotalNumberOfBytes as Int64 you can covert their values in division expression like this: ((float)lpTotalNumberOfFreeBytes / (float)lpTotalNumberOfBytes )*100; However this expression return float value so you need to change type of strAnswer from Int64 to float or convert explicitly convert result to Int64: Int64 strAnswer = (Int64) (((float)lpTotalNumberOfFreeBytes / (float)lpTotalNumberOfBytes )*100); But remember if you convert float to Int64 system will cut off everything behind decimal point, so in this particular case result 5.7687068 will be converted to 5. hth

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups