Jump to content

Easy way to solve end result of 'for loop'?


Hartja

Recommended Posts

I understand For loops and all of their components, but I was wondering if anyone has a trick to solve the end result?

My wording is probably confusing so let me type out an example-(I apologize if there is a format error, I am new here)

 

double x = 0;

for (double i = 32; i < 36; i += 0.25)

x += i % 32;

out.println(x);

 

I am on the computer science team at my school and I have trouble with these types of problems. I can just hand write it step by step but that takes way too long, does anyone know of a way to quickly solve it without a computer?

 

Thank you.

Link to comment
Share on other sites

Does it depend on rounding error? If it does, there is no way to figure it out in your head. You must know how double is represented on the computer to know whether there is rounding error. If not, the final value of i is easy to deduce.

Link to comment
Share on other sites

I understand For loops and all of their components, but I was wondering if anyone has a trick to solve the end result?

 

My wording is probably confusing so let me type out an example-(I apologize if there is a format error, I am new here)

 

double x = 0;

for (double i = 32; i < 36; i += 0.25)

x += i % 32;

out.println(x);

 

I am on the computer science team at my school and I have trouble with these types of problems. I can just hand write it step by step but that takes way too long, does anyone know of a way to quickly solve it without a computer?

 

Thank you.

What computer language is that?

Link to comment
Share on other sites

36-32=4

4/0.25 = about 16 iterations, depending on whether there is rounding. The final value of i is either 36 or about 36.24.

 

The question seems intended to show whether a person understands the type double.

Edited by EdEarl
Link to comment
Share on other sites

This is what I got if anyone feels like taking a look:

 

32.0 -i-
0.0 -x-
1 -added for iteration count: n++ -
32.25
0.25
2
32.5
0.75
3
32.75
1.5
4
33.0
2.5
5
33.25
3.75
6
33.5
5.25
7
33.75
7.0
8
34.0
9.0
9
34.25
11.25
10
34.5
13.75
11
34.75
16.5
12
35.0
19.5
13
35.25
22.75
14
35.5
26.25
15
35.75
30.0

16

 

 

 

I do think you are probable right about the original question testing knowledge.

 

I think OP is asking if there is any way to skip ahead for the final x value though. Not sure on that one and whether the solution would be generally applicable.

 

Late and my engine is rapidly running out of steam here. Enjoy.

Edited by Endy0816
Link to comment
Share on other sites

I do think you are probable right about the original question testing knowledge.

 

Or (if it were an interview question) testing how the person thinks about the problem. Getting the "right" answer might not matter but I might give more consideration to someone who made points about representations of floating point numbers and attempted an analytical solution like EdEarl's.

Link to comment
Share on other sites

Instructors like to have students find the result by hand when their students first start working with loops. I'm guessing op is just trying to find shortcuts.

 

32 + (15 * 0.25) is easy enough to figure out, but am not sure if the summation needed to find for x can be simplified.

Link to comment
Share on other sites

Integer values and 1/2n don't BUT the compiler is often inaccurate when converting "0.25" because this is written in radix-10 and 5 dos not divide the powers of 2.

 

So the double representation of "0.25" may well differ a little bit and end with 111111..., which suffices to foil the "i<36" test.

 

I'm surprized by the "i % 32" that apparently returns 1 everytime. It should have worked among doubles according to

http://mindprod.com/jgloss/modulus.html

but what happens in Java during the type conversion? 32 is some sort of int.

 

This program is a collection of what shouldn't be done.

Link to comment
Share on other sites

I know by experience that the best compilers convert decimal constants like 0.25 approximately in binary floats. This isn't even a mistake from them: they're within the granted tolerance. The programmers have to know it, not the compilers.

 

It comes from 0.1 having no exact representation in binary, because 5 doesn't divide 2. Just like 1/3 has no exact representation in radix 10: it's 0.333 approximately, and it remains approximate however many places you take. Then, if you multiply by 3, you get 0.999 and not 1. If your arithmetic computes to three decimal places, the result is correct within the accepted tolerance, and it's the programmer's duty to live with that.

 

Now, a compiler that reads the constant zero dot two five is likely to

  • Load 5 (as a binary doublefloat)
  • Multiply by 0.1 <= This is already inaccurate because 0.1 has no exact representation
  • Add 2
  • Multiply by 0.1 <= Again a source of inaccuracy

Dividing by 10 would improve nothing because all the binary machines I know make doublefloat divisions by first computing a reciprocal and then a multiplication.

 

A compiler can't neither recognize all constants that have an exact representation as binary doublefloat because there are too many.

 

The result is approximative as a consequence. It is allowed to by approximative. And I have observed it. It fully explains the loop's behaviour here.

 

What seems to work up to now is that integer constants are well represented by doublefloats in javascript.

 

----------

 

Can someone tell us how Java converts the datatypes when asked to perform that?

double % litteral_int

 

To my opinion, something fails there too.

Link to comment
Share on other sites

  • 2 weeks later...

The Ieee 754 is widely available and described. Unfortunately, the problem is not the binary representation of 0.25, which is exact, but the conversion from the string "0.25" to a binary float, which is allowed to be inaccurate within the tolerance, is expected to be inaccurate because of the conversion algorithm, and which I observed to be inaccurate in Javascript.

 

----------

 

Can someone tell what goes wrong with the other line?

i % 32

And first, how does Java behave with the double float and the int?

Link to comment
Share on other sites

  • 3 weeks later...

Undefined ...

 

the modulo operator gives an int, the regression from a double to int can be system specific i think.

 

using a bit field and quotient should indicate the implementation.

 

long double promption, double promotion, float promotion etc, always losing precision.

 

Its relative to your system, each type has specified memory allocated (as in 32it float)

 

as for an algraic formula im at a loss, if you wanted to estimate the iterations then using compound interest formula with the modulo 32.25 ^ x might be some help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.