Public Group

# [solved] strange error with floats or doubles

This topic is 3925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone! I have a little problem with my code. I have created a small application to display quadratic NURBS in 2D. The control points and the knot vector of the curve are defined in the following lines: float points[][] = {{/*x*/10, /*y*/10, /*weight*/1}, {410, 10, 0.5}, {410, 410, 1}}; float knotVector[] = {0, 0.2, 0.4, 0.6, 0.8, 1}; The code works perfectly, but when i replace, for example, "0.5" with "1/2" or "0.2" with "1/5" the program stops running and it doesn't display the curve. Do you have any idea of what's wrong? Thanks. [Edited by - smeyel on February 16, 2008 6:43:40 AM]

##### Share on other sites
The expression 1/2 performs an integer division. That is, the integer 1 divided by the integer 2. Because of this, the result is also an integer. Since integers cannot express decimal numbers, the result is rounded down. In this case, both 1/2 and 1/5 are rounded down to 0.

To perform a floating-point division, either cast one or both of the operands to a double, or append .0 to either (or both) number(s).

eg.
1.0/2.0, 1.0/5.0

This will correctly generate 0.2 and 0.5.

##### Share on other sites
The expression is being evaluated as integer math before being assigned to float storage. Change the integer literals to floats.

1 / 2 => 1.0 / 2.0 or 1.0f / 2.0f

##### Share on other sites
Thank you and sorry for the stupid question :D

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011317
• ### Who's Online (See full list)

There are no registered users currently online

×