-4

What I'm trying to do is to increment the entire float value, for example:

Incrementing

-0.09683964401

to:

1.09683964401

or:

1.09683964401

to:

2.09683964401

Decrementing

-0.09683964401

to:

-1.09683964401

or:

-1.09683964401

to:

-2.09683964401

I know I would need to use a loop, but how would I do it?

This is what I tried:

(float)myFloatValue++;

which doesn't do what I actually need.

Community
  • 1
  • 1
  • What does that code do that you don't need? – nicomp Apr 23 '16 at 12:53
  • @nicomp should I use it in a loop? How should I do a loop? Replying to your question: it doesn't increment the entire float value. – xF00lProgram Apr 23 '16 at 12:54
  • 1
    Btw `-0.09 + 1` is not `1.09` but `0.91`... – Jarod42 Apr 23 '16 at 12:55
  • You say "What I'm trying to do is to increment the entire float value," - but your examples show incrementing only the integer part; and leaving the absolute value of the floating part unchanged – M.M Apr 23 '16 at 12:55
  • 1
    I don't know what that means. "It doesn't increment the entire float value"? What, precisely, does it do? What does the entire code snippet look like? – nicomp Apr 23 '16 at 12:55
  • @M.M **that was an example. Didn't want to use the calculator to increment everything**... My question was how could I increment the **entire** float value, so other than the integer part, the absolute value goes incremented too. – xF00lProgram Apr 23 '16 at 12:59
  • @xF00lProgram "incrementing the entire float value" on `-0.09683964401` gives `0.90316035599` – M.M Apr 23 '16 at 13:02
  • @M.M ok, in this case the first (integer) part doesn't changes. How could I integrate this in C++? – xF00lProgram Apr 23 '16 at 13:04
  • @xF00lProgram sorry I have no idea what you mean. Perhaps you could give some more examples. – M.M Apr 23 '16 at 13:05

2 Answers2

0

The behaviour in your examples could be reproduced by:

void increment(float& f)
{
    if ( f > -1 && f < 0 )
        f = -f;
    ++f;
}

void decrement(float& f)
{
    if ( f > 0 && f < 1 )   // Presumably; you didn't show any examples in this range
        f = -f;
    --f;
}
M.M
  • 138,810
  • 21
  • 208
  • 365
0

You don't need a loop to increment or decrement any number.Either use the ++ operator

myFloatValue++

or

myFloatValue = myFloatValue + 1

This will increment the entire number and not just the integer part.And if want to increment only the integer part then

myFloatValue = myFloatValue < 0 ? (-myFloatValue  + 1) : (myFloatValue +1);
XZ6H
  • 1,779
  • 19
  • 25