Author Topic: My math skills are insufficient...  (Read 604 times)

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
My math skills are insufficient...
« on: September 10, 2011, 04:59:51 pm »


               I have a positive float with an unknown value. (User input)
I need to get the numbers after the dot as a integer.

examples:

00.65 -> 65
24.38 -> 38
98.00 -> 0


This is not nwscript related. (Its C++)

Any help appreciated.
               
               

               


                     Modifié par Xardex, 10 septembre 2011 - 04:00 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
My math skills are insufficient...
« Reply #1 on: September 10, 2011, 05:14:43 pm »


               Convert to string and take everything from dot right. Not sure how or if its possible to convert it into string or char though.
               
               

               


                     Modifié par ShaDoOoW, 10 septembre 2011 - 04:15 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
My math skills are insufficient...
« Reply #2 on: September 10, 2011, 05:31:49 pm »


               here is how I would do it in NWScript.  Assuming that the number is inputed as a float to get the forst two decimals.  

int Get2decimalsAsInt(float input)
{
  input = (input- FloatToInt( input)) * 100;
  return  FloatToInt(input);
}

Hope that helps.
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
My math skills are insufficient...
« Reply #3 on: September 10, 2011, 06:02:20 pm »


               I ended up using messing around with 'static_cast <newt type> (value)'

Thanks for the help anyway!
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
My math skills are insufficient...
« Reply #4 on: September 10, 2011, 06:25:44 pm »


               <works through it on his fingers...>

Lightfoot8 wrote...
...
int Get2decimalsAsInt(float input)
{
  input = (input- FloatToInt( input)) * 100;
  return  FloatToInt(input);
}
...

Nicely done :-)

<...and doesn't even need to take off his boots!>
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
My math skills are insufficient...
« Reply #5 on: September 10, 2011, 06:27:23 pm »


               Lightfoot: this really works? Thought it will round up the float again.
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
My math skills are insufficient...
« Reply #6 on: September 10, 2011, 07:44:06 pm »


               <stepping on...>

ShaDoOoW wrote...

Lightfoot: this really works? Thought it will round up the float again.

It should work like this (assuming float input of yy.xxxx)
:
1) FloatToInt( input) evaluates as  yy.xxxxx -> int (yy)
2) (input- int( yy )) evaluates as (yy.xxxx - yy) -> float (.xxxxx)
3) (float (.xxxxx)) * 100 evaluates as (.xxxx)*100 -> float (xx.xx)
and therefore:
4) return FloatToInt(input); evaluates as return FloatToInt(xx.xx) -> return int (xx)

input = (input- FloatToInt( input)) * 100;
return FloatToInt(input);

Elegant, IMO :-)

<...the cat's toes>
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
My math skills are insufficient...
« Reply #7 on: September 10, 2011, 07:51:17 pm »


               

ShaDoOoW wrote...

Lightfoot: this really works? Thought it will round up the float again.


I do not follow what you are saying.   FloatToInt  does not round.  

And I guess I lied when I said I would do it that way in NWScript.  I only did it that way because I do not know if C++ uses the operators the same way and I do not know if he wants the +\\- state of the original number preserved.  what I would use would look more like.

int Get2decimalsAsInt(float input)
{
   input = input * 100 ;
   return FloatToInt(input) % 100;
}
               
               

               


                     Modifié par Lightfoot8, 10 septembre 2011 - 07:19 .
                     
                  


            

Legacy__six

  • Hero Member
  • *****
  • Posts: 1436
  • Karma: +0/-0
My math skills are insufficient...
« Reply #8 on: September 11, 2011, 01:48:08 am »


               Going from ShaDoOoW's method, as Lightfoot's will only work with a set number of decimal places...

int decsAsInt(float val)
{
   stringstream temp;
   temp << val;
   string Str1 = temp.str();

   int pos = Str1.find('.');
   Str1.replace(0, pos+1, "");

   return atoi(Str1.c_str());
}

I hate wrestling with types in c++ too. There's probably a much nicer way to convert than using a stringstream, but if there is it's apparently a lot harder to remember '<img'>
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
My math skills are insufficient...
« Reply #9 on: September 11, 2011, 12:17:58 pm »


               I didn't say it but the input would only have a max of two decimals. I did it in a fashion similiar to Lightfoot's method, though his method would work too.
               
               

               


                     Modifié par Xardex, 11 septembre 2011 - 11:18 .
                     
                  


            

Legacy_Axe_Murderer

  • Full Member
  • ***
  • Posts: 199
  • Karma: +0/-0
My math skills are insufficient...
« Reply #10 on: September 12, 2011, 11:07:32 pm »


               fmod(x,1) *100
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
My math skills are insufficient...
« Reply #11 on: September 12, 2011, 11:19:43 pm »


               <looks on...>

Axe_Murderer wrote...

fmod(x,1) *100

Sweet! :=)

<...with big eyes>