Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,629 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,031 people online right now. Registration is fast and FREE... Join Now!




% with float problems

 
Reply to this topicStart new topic

% with float problems

Noldona
post 28 Aug, 2006 - 01:27 PM
Post #1


Addicted to DIC

****
Joined: 2 Jul, 2002
Posts: 910



Thanked 2 times
My Contributions


I do not understand why I am getting this error. I am using Visual Studios .NET 2005 MC++. The error I am getting is '%' : illeal, left operand has type 'float'. I have tried this with both a float and a double and it give me basically the same error. I don't understand this when the example the MSDN gives for this operator is "5.6 % 0.5 returns 0.1." Clearly floating-point numbers will work with this operator, just not for me however.

CODE

float temp, result;

temp = Convert::ToSingle(txtCP->Text);
result = temp % 1;
if (result != 0) {
     temp -= result;
     txtCP->Text = Convert::ToString(temp);
}


The result = temp % 1; line is the one I am getting the error one. Can anyone help me out with this one.
User is offlineProfile CardPM

Go to the top of the page

Videege
post 28 Aug, 2006 - 06:35 PM
Post #2


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


umm...give this a shot. Initialize both temp and result to 0.0.
User is offlineProfile CardPM

Go to the top of the page

BitByte
post 29 Aug, 2006 - 04:37 AM
Post #3


D.I.C Head

**
Joined: 9 Aug, 2006
Posts: 194



Thanked 2 times
My Contributions


QUOTE(Noldona @ 28 Aug, 2006 - 02:27 PM) *

I do not understand why I am getting this error. I am using Visual Studios .NET 2005 MC++. The error I am getting is '%' : illeal, left operand has type 'float'. I have tried this with both a float and a double and it give me basically the same error. I don't understand this when the example the MSDN gives for this operator is "5.6 % 0.5 returns 0.1." Clearly floating-point numbers will work with this operator, just not for me however.

CODE

float temp, result;

temp = Convert::ToSingle(txtCP->Text);
result = temp % 1;
if (result != 0) {
     temp -= result;
     txtCP->Text = Convert::ToString(temp);
}


The result = temp % 1; line is the one I am getting the error one. Can anyone help me out with this one.



The modulus operator can only be applied to int, short, char, bool, long signed or unsigned.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 29 Aug, 2006 - 05:20 AM
Post #4


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Well spotted BitByte, I was about to say the same thing until I realised you did the job.

Since the Result of the % operation must always be positive, it must work on non-float numbers as well.
User is offlineProfile CardPM

Go to the top of the page

Noldona
post 30 Aug, 2006 - 07:54 AM
Post #5


Addicted to DIC

****
Joined: 2 Jul, 2002
Posts: 910



Thanked 2 times
My Contributions


Since M$ in their "infinite wisdom" (note the sarcasm) decided not to allow floats and doubles with the % operator even though they used a float int he example on MSDN, I tried a different way. I converted the float to an int and set that result to an int variable. I then subtracted the int from the float to get the result and when on with the rest of the code. Well this worked up to a point. Again M$'s "infinite wisdom" strikes with the Convert::ToInt32 returning the nearest EVEN integer. So 4.5 returns 4 while 5.5 returns 6. What were they thinking?!?!? So now I have to use an even more round about way off doing it. They have rekindled my hatred for M$ with an even bigger flame now.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 31 Aug, 2006 - 06:07 AM
Post #6


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


The simple typecast int x = (int) 3.6; doesn't round the number.

Don't use such functions (or write your own implementation) when there's an easier way to do it.

Remember, Simplicity is the Key smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month