-1

How can I transform rational numbers like 1.24234 or 45.314 into integers like 124234 or 45314 also getting the number of decimal digits?

fpiro07
  • 847
  • 1
  • 13
  • 18
  • How are the original numbers stored/represented ? – Paul R Dec 17 '12 at 22:32
  • 3
    Simple algorithm: remove the decimal point... –  Dec 17 '12 at 22:34
  • I've tried with `double n = 45.314; int i = 0; for (; int(n) != n; i++, n *= 10) { }` But it doesn't work because the dobule are not that precise to do a `==` comparison – fpiro07 Dec 17 '12 at 22:35
  • 2
    @fpiro07 in this case, your question doesn't even make sense. You can't say that `1.234` is really `1.234` because it's not guaranteed that it can be exactly represented by a `float`. It may be `1.23399999374` as well... –  Dec 17 '12 at 22:37
  • 2
    `45.314` is not the decimal number `43.314`, but rather a floating point approximation of it. Try doing `char const* str = "43.314";`, then `for(char const* it=str; *it; ++it) { if (*it != '.') std::cout << *it; }` for an example of a method that works. However, this will depend heavily on what your input format is. – Yakk - Adam Nevraumont Dec 17 '12 at 22:37
  • @H2CO3 Ok... And what would your "simple algorithm" be? – fpiro07 Dec 17 '12 at 22:37
  • 1
    @fpiro07 just what Tony Hopkinson answered. –  Dec 17 '12 at 22:38
  • Cleverer than the one I posted maybe. – Tony Hopkinson Dec 17 '12 at 22:38
  • @Yakk Oh I see... My input is a double but I could convert it into a string, then remove the point and then convert back into a number... Could it work? – fpiro07 Dec 17 '12 at 22:39
  • Depends on the conversion if double to string chucks it out as scientific, it won't. Also if you are doing floating point, it's inherent inaccuracy will give you crap like 1 - (1/3) = 0.00000000001543 x 10 ^ 28 and such – Tony Hopkinson Dec 17 '12 at 22:43
  • 1
    If your input is a `double`, the answer is "the question makes no sense". `double`s are rational numbers of the form x/2^n, where `x` is an integer (plus some unusual states, like `NaN` and infinity and `-0`. They are not numbers of the form `12.345` -- there is no representation of `12.345` as a `double`, there are just approximations. You could approximate an answer, but it would be merely an approximation. Misunderstanding this leads to many, many problems. – Yakk - Adam Nevraumont Dec 17 '12 at 22:45
  • At this point I don't really understand how can exist things like calculators or mathlab... – fpiro07 Dec 17 '12 at 22:53
  • I said it makes no sense if you use a `double` as input. You specified the input type. If your input type can represent base-ten values, there isn't a problem. Or, if you are willing to accept approximations. As an example, a calculator takes as input button pushes from a user of digits from 0 to 9 -- that isn't a `double`. They might do math internally as `double` types (or not!), but if so they choose to round and do approximations at various points. – Yakk - Adam Nevraumont Dec 17 '12 at 22:58
  • And do you know where can I find base-ten floating point types? – fpiro07 Dec 17 '12 at 23:00
  • @fPiro. It's simple if you want accuracy, don't use floating points. Have a look for a C++ , fixed decimal point library. – Tony Hopkinson Dec 17 '12 at 23:00
  • Well, strings represent base-ten floating point values pretty well. :) Adding them takes work. If you are working with dollars and cents, using `int`s in pennies works well. – Yakk - Adam Nevraumont Dec 17 '12 at 23:15

3 Answers3

1

Convert to a string

Find the position of the decimal point.

Subtract that from the length of the above string, for the number of decimals.

Then take the point out of the string.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0
int i=0;
float a = 1.24234;

for(i; i<20; i++){
    float b=pow(10,i);
    if((a*b)%10==0)
        break;
}
int c = pow(10,i-1);
int result = a*c;

I think this code will help you.

Candost
  • 1,029
  • 1
  • 12
  • 28
0

If your number is W.D (Whole.Decimal)

To get W just do (int)W.D. To get D you can do W.D - (int) W.D

Now you have your whole number and your decimal point separated. To figure out your x10 multiplier on your W keep dividing D by 10 until you get a result that is less than 10.

Now: WxN+D (where N is the number of times you divided by 10)

Note: I didn't write the code as an example, because I feel this may be a homework assignment. Also, if you are using very long (ie: precise floating points) this won't hold, and could likely overflow. Check your bounds before implementing something like this.

David D
  • 1,571
  • 11
  • 12