CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Dec 2011
    Posts
    8

    Cleanest way to check if a number is an integer.

    So I need to find factors of my numbers to speed up my algorithm and I'm dividing by prime factors and to find them I obviously need to know if they are still integers after I divide into each prime. For 2 and 3 this is easy, since I can do simple bit operations before I divide. For any prime | prime > 3, I want to do a brute force approach and check if my result is an integer or a fraction.

    I thought about this and I could use %, if % > 0, I have a fraction.

    I could put the result in an integer and in a float, if float and integer differ, I have a fraction.

    I could simply use a float, do a bit operation on the exponent part (bits 2-9 iirc) to see if the float has a decimal.

    I could use Math.Truncate(double) from the .Net library which takes out the integer part for me, if I check thsi against the double and if it's not 0, I have a fraction.

    Which method would you use?
    Last edited by Candystore; December 6th, 2011 at 04:50 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured