Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Removing fractional part of a number

  • 11-09-2019 12:58pm
    #1
    Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭


    Is there a way to remove the fractional part of a number without using the mod function or converting to an integer?

    I.e. I want to change 2314.67 to 2314.0.


    Is should say I am using a very basic tool.


    numerator := 2314.67
    int denominator := 1
    int output := 1

    output := 2314.67 / 1

    output which was declared as a int (due to lack of decimal in assignment) now takes on the floating point value of the division result.

    Would be simple with a mod function or cast to an int. But alas that does not work. The tool does not seem to hold the declared variable in its initial declared type.


Comments

  • Posts: 3,689 ✭✭✭ [Deleted User]


    In Java / C++ / .......?


  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    Nope proprietary test interface.

    Was just wondering if there was a mathematical way to do this before I go ask if the owner will add a mod function or request permission for inclusion of a tolerance on my expected results versus the actual returned values.


  • Registered Users, Registered Users 2 Posts: 9,514 ✭✭✭TheChizler


    A bitwise AND?


  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    TheChizler wrote: »
    A bitwise AND?

    The location of the fractional part is not consistent with floating point numbers as the resolution changes as the numbers varies in size. So I don't think bitwise manipulation will work here.
    I think the easiest thing is to introduce a tolerance.


  • Registered Users, Registered Users 2 Posts: 1,681 ✭✭✭Apiarist


    It depends on what capabilities are in the tool. You can find the integer number 2314 by employing some search algorithm which compares a test integer to the floating point number. You could convert the float to a string, cut out ".67" and convert back to int. Or may be the tool can do HTML requests? Then create an online "mod" function, send the float to your server and get back an int.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,681 ✭✭✭Apiarist


    Nope proprietary test interface....before I go ask if the owner will add a mod function .....

    Does it run on a PC? If so, tell them to use some common shell, like Tcl shell.


  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    Convert to a string
    Parse for '.'
    Convert back to num

    Yet no modulus operator exists!?! :pac:


    So how do I mark this solved?


  • Moderators, Society & Culture Moderators Posts: 15,812 Mod ✭✭✭✭smacl


    Is there a way to remove the fractional part of a number without using the mod function or converting to an integer?

    I.e. I want to change 2314.67 to 2314.0.


    Is should say I am using a very basic tool.


    numerator := 2314.67
    int denominator := 1
    int output := 1

    output := 2314.67 / 1

    output which was declared as a int (due to lack of decimal in assignment) now takes on the floating point value of the division result.

    Would be simple with a mod function or cast to an int. But alas that does not work. The tool does not seem to hold the declared variable in its initial declared type.

    Looks like Pascal but not. Answer is language dependant, you have functions like floor, ceil and fmod in C or C++ and Round, Frac, Trunc in Pascal. Rounding can also be a minefield, see the following SO question.


  • Registered Users, Registered Users 2 Posts: 6,287 ✭✭✭Talisman


    numerator := 2314.67
    int denominator := 1
    int output := 1

    output := 2314.67 / 1
    If the code is some form of Pascal you can use the div operator. It performs the division operation and discards the fractional part.

    2314.67 div 1 => 2314

    Alternatively you could just truncate the numerator using the Trunc function.

    Trunc(2314.67) => 2314

    The Round function will round up for your given example.

    Round(2314.67) => 2315


  • Registered Users, Registered Users 2 Posts: 768 ✭✭✭14ned


    I'm super late to the reply, sorry, but in C the function for stripping off the fractional part is trunc():

    https://en.cppreference.com/w/cpp/numeric/math/trunc

    You can implement this using bit masking, just mask off the fractional part. But trunc() does exactly that for you, so no need.

    Also, in C a simple cast via an integer would work, but note that larger double values not representable in a 64 bit integer.


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 15,812 Mod ✭✭✭✭smacl


    14ned wrote: »
    You can implement this using bit masking, just mask off the fractional part.

    How exactly would you do that, given the floating point number is represented as exponent and mantissa in binary and not decimal?


  • Registered Users, Registered Users 2 Posts: 768 ✭✭✭14ned


    smacl wrote: »
    How exactly would you do that, given the floating point number is represented as exponent and mantissa in binary and not decimal?

    IEEE754 is ultimately just a bitscan followed by a bitshift of the significand with a few corner case special handling. This is to make it fast to hardwire into transistors, but it can be done by hand quite efficiently. Indeed, back when I began, floating-point hardware was emulated by the CPU.

    A quick Google search turns up the algorithm at https://stackoverflow.com/questions/12342926/casting-float-to-int-bitwise-in-c/12343933#12343933. I haven't checked it, but it looks about right.

    I would urge people to choose trunc(), or ceil() or floor() instead of writing their own soft floating-point implementation in C. Though, great learning experience of course.

    Niall


Advertisement