Subject:
|
RE: Converting a float to an integer
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Thu, 9 May 2002 10:39:35 GMT
|
Viewed:
|
2442 times
|
| |
| |
I have built a plotter and want to draw a diagonal line of varying angles.
It requires one motor to run slower then the other at a ratio based on the
distance it has to travel compared to the other one.
So I do something like this
xspeed = (int) MAX_SPEED*(xdist/ydist);
motor_c_speed(xspeed);
lcd_int(xspeed); lcd_refresh();
The LCD always displays a "0" and of course the motor stops. I have
outputted each variable separately and the result is not 0. So I figure it
is a float to an integer problem.
Turns out the problem was the brackets around xdist/ydist... Take them out
and it works fine..
xspeed = (int) MAX_SPEED*xdist/ydist;
???
Michael.
-----Original Message-----
From: news-gateway@lugnet.com [mailto:news-gateway@lugnet.com] On Behalf
Of Dean Hystad
Sent: Thursday, 9 May 2002 2:11 AM
To: lugnet.robotics.rcx.legos@lugnet.com
Subject: Re: Converting a float to an integer
In lugnet.robotics.rcx.legos, Michael Marklew writes:
> Hopefully this isnt a silly question. Does anyone know how to covert a float
> to an integer? It doesn't appear that the function atoi exists.
>
> Michael.
float f = 1.4;
int i = (int)((i >= 0) ? i + 0.5 : i - 0.5);
OR
if (f >= 0)
i = (int)(i + 0.5);
else
i = (int)(i - 0.5);
This is assuming you want to round to the nearest integer.
Just curious, where are you getting the ascii strings that you need to
convert to int's? Does legOS support standard I/O? If so, what is stdin
and stdout? Are files supported, and if so were do the files reside?
Good Luck,
Dean Hystad
|
|
Message has 1 Reply: | | Re: Converting a float to an integer
|
| It looks like xdist and ydist are integers. You divide an integer by an integer, the answer is an integer. For example, if xdist=5 and ydist=10, you have 5/10, the answer is 0 (with a remainder of 5). So, if you do the multiply first, you will be (...) (23 years ago, 13-May-02, to lugnet.robotics.rcx.legos)
|
Message is in Reply To:
| | Re: Converting a float to an integer
|
| (...) float f = 1.4; int i = (int)((i >= 0) ? i + 0.5 : i - 0.5); OR if (f >= 0) i = (int)(i + 0.5); else i = (int)(i - 0.5); This is assuming you want to round to the nearest integer. Just curious, where are you getting the ascii strings that you (...) (23 years ago, 8-May-02, to lugnet.robotics.rcx.legos)
|
4 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|