Aller au contenu

Photo

My math skills are insufficient...


  • Veuillez vous connecter pour répondre
11 réponses à ce sujet

#1
Xardex

Xardex
  • Members
  • 217 messages
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 .


#2
Shadooow

Shadooow
  • Members
  • 4 471 messages
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 .


#3
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
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.

#4
Xardex

Xardex
  • Members
  • 217 messages
I ended up using messing around with 'static_cast <newt type> (value)'

Thanks for the help anyway!

#5
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<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!>

#6
Shadooow

Shadooow
  • Members
  • 4 471 messages
Lightfoot: this really works? Thought it will round up the float again.

#7
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<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>

#8
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

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 .


#9
_six

_six
  • Members
  • 919 messages
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 :P

#10
Xardex

Xardex
  • Members
  • 217 messages
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 .


#11
Axe_Murderer

Axe_Murderer
  • Members
  • 279 messages
fmod(x,1) *100

#12
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<looks on...>

Axe_Murderer wrote...

fmod(x,1) *100

Sweet! :=)

<...with big eyes>