Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
nielsm
Jun 1, 2009



minato posted:

After some reading and discussion, the issue I had with ServiceFinder being effectively global may not be such an issue after all. The nuance that was eluding me was that it's not important to make every single class in the app testable via dependency injections, just the classes that do meaningful work.

If you see software testing as a kind of (extremely weak) mathematical proof, it should also be clear that at some point you have to assume some axioms. It doesn't make sense to test everything, otherwise you would arguably also have to test that your operating system and hardware was keeping time right, etc.

Adbot
ADBOT LOVES YOU

raminasi
Jan 25, 2005

a last drink with no ice
Newbie Prolog question:
code:
add(A, B, Res) :- Res = A + B.
code:
12 ?- add(1, 1.5, Ans).
Ans = 1+1.5.
How do I get that to be Ans = 2.5?

delta534
Sep 2, 2011

GrumpyDoctor posted:

Newbie Prolog question:
code:
add(A, B, Res) :- Res = A + B.
code:
12 ?- add(1, 1.5, Ans).
Ans = 1+1.5.
How do I get that to be Ans = 2.5?


Make it
code:
add(A,B,Res):- Res is A + B.

Tots
Sep 3, 2007

:frogout:
I'm reading through my Java book and one of the end of chapter questions has this example code in which I'm supposed to find the error:

code:
        for (k = .1 ; k != 1.0 ; k += .1)
            System.out.println( k );
Easy enough, k was never initialized... but wait! There appears to be a much more insidious error which I doubt the author intended.

The output of

code:
        for (double k = .1 ; k != 1.0 ; k += .1)
            System.out.println( k );
is:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
1.2
1.3
1.4000000000000001
1.5000000000000002
1.6000000000000003
1.7000000000000004
1.8000000000000005
1.9000000000000006
2.0000000000000004
...ad infinitum.

Can anyone tell me what the hell is going on here?

Look Around You
Jan 19, 2009

Tots posted:

I'm reading through my Java book and what of the end of chapter questions has the example code in which I'm supposed to find the error:

code:
        for (k = .1 ; k != 1.0 ; k += .1)
            System.out.println( k );
Easy enough, k was never initialized... but wait! There appears to be a much more insidious error which I doubt the author intended.

The output of

code:
        for (double k = .1 ; k != 1.0 ; k += .1)
            System.out.println( k );
is:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
1.2
1.3
1.4000000000000001
1.5000000000000002
1.6000000000000003
1.7000000000000004
1.8000000000000005
1.9000000000000006
2.0000000000000004
...ad infinitum.

Can anyone tell me what the hell is going on here?

Floating point numbers are hard. (a more general article)

They're pretty imprecise because you're trying to fit an uncountably infinite amount of numbers into a finite set of bits to represent them.

e:
This is why you never want to test straight equality of floating point numbers. Always test if it's in a small range of the number.

e2: The following code ONLY prints out "a ~~ d" on my machine.
code:
#include <stdio.h>
#include <math.h>

int main(void)
{
	double a;
	double b;
	double c;
	double d;
	
	a = 0.1;
	b = -0.2;
	c = 0.3;
	d = b + c;

	//     V this is WRONG 
	if (a == d) { 
		/* do something */
		printf("%f == %f\n", a, d);
	}

	// This is better
        // (made a change)
	if (fabs(a - d) < 0.005) {
		printf("%f ~~ %f\n", a, d);
	}

	return 0;
}
e2:
Added a bit more to code listing, added output.

Look Around You fucked around with this message at 07:29 on Feb 26, 2012

Tots
Sep 3, 2007

:frogout:
I'm trying to figure out a way to return the expected output based on that same for structure, but I can't think of anything that will work and not be absolutely retarded. Any ideas?

Look Around You
Jan 19, 2009

Tots posted:

I'm trying to figure out a way to return the expected output based on that same for structure, but I can't think of anything that will work and not be absolutely retarded. Any ideas?

Like I said in the last post, never test for strict (in)equality on floating point values (float or double). Check out my code that I posted for a bit better way of handling it in general. If you know you're approaching it from one side you can use a single less than (or greater than) rather than bracketing it though.

e:
The bracketing "equality" test for doubles that I did in the second if block could easily be refractored out into it's own function, btw.

Look Around You fucked around with this message at 07:15 on Feb 26, 2012

Nippashish
Nov 2, 2005

Let me see you dance!

Look Around You posted:

Like I said in the last post, never test for strict (in)equality on floating point values (float or double). Check out my code that I posted for a bit better way of handling it in general. If you know you're approaching it from one side you can use a single less than (or greater than) rather than bracketing it though.

How about abs(a-d) < 0.005, which tests from both sides? This version of "equality" also has the annoying property of being non-transitive.

Look Around You
Jan 19, 2009

Nippashish posted:

How about abs(a-d) < 0.005, which tests from both sides? This version of "equality" also has the annoying property of being non-transitive.

That's true, I was honestly just being a bit lazy there. I also didn't want to #include <math.h> to use fabs() (again, i was lazy). That is a better approach though.

e: changed it in the code above

Look Around You fucked around with this message at 07:29 on Feb 26, 2012

Tots
Sep 3, 2007

:frogout:
I get what you're all saying, but I was trying to achieve the exact output of .1 - .9 using that same basic for structure somehow... just because. Anyway, I thought about it for like half an hour then gave up after getting frustrated... My brain is sort of goop at this point from lack of sleep.


Anyway, turns out that the solution in the book doesn't actually say anything about initializing the value, rather it says "Don't use floating point numbers for a counter you fucktard." :downsgun:

code:
    public static void main(String[] args) {

        for (int k = 1 ;  k != 10 ; k += 1)
        {
            System.out.println( (double) k / 10);         
        }
        
    }
E: Just so you get an idea, I was trying to build a separate method that would take a value (say double 0.1) then compare it to a range (.05-.09 in this case) and then return .1 if it did fall within that range, I was also trying to make it to apply to any number that happened to be passed to the method, and somehow have it figure out the appropriate range to compare it to. Then when it returned the .1 (or whatever) again it would somehow have to magically not be hosed up like the double it originally evaluated. I can't wait to look at this gem a year down the road.

Tots fucked around with this message at 07:56 on Feb 26, 2012

Look Around You
Jan 19, 2009

Tots posted:

I get what you're all saying, but I was trying to achieve the exact output of .1 - .9 using that same basic for structure somehow... just because. Anyway, I thought about it for like half an hour then gave up after getting frustrated... My brain is sort of goop at this point from lack of sleep.


Anyway, turns out that the solution in the book doesn't actually say anything about initializing the value, rather it says "Don't use floating point numbers for a counter you fucktard." :downsgun:

code:
    public static void main(String[] args) {

        for (int k = 1 ;  k != 10 ; k += 1)
        {
            System.out.println( (double) k / 10);         
        }
        
    }

This would work.

If you're using Java 1.5+ you probably have a printf function:
Try System.out.printf("%.1f\n", k); (where k in here is a double).

Tots
Sep 3, 2007

:frogout:

Look Around You posted:

This would work.

If you're using Java 1.5+ you probably have a printf function:
Try System.out.printf("%.1f\n", k); (where k in here is a double).

That prints
1.0
2.0
etc...

I really need to sit down one night and learn all the printf poo poo. It looks ridiculously useful.

This works :haw:

code:
        for (int k = 1 ;  k != 10 ; k += 1)
        {
            System.out.println("0." + k);       
        }

Tots fucked around with this message at 08:31 on Feb 26, 2012

Look Around You
Jan 19, 2009

Tots posted:

That prints
1.0
2.0
etc...

I really need to sit down one night and learn all the printf poo poo. It looks ridiculously useful.

This works :haw:

code:
        for (int k = 1 ;  k != 10 ; k += 1)
        {
            System.out.println("0." + k);       
        }

I'm guessing you know that's not what you're looking for though, right?

The printf I had will work, but you need to cast your int into a double and divide by 10 again if you want to get the 0.1 etc... you were just passing it 1, 2, 3, [...] which got cast to doubles when passed to printf which is why it came out as 1.0, 2.0 [...]


e:

quote:

I just had another thought. You said that when testing for equality in floating point #'s you should use a range. Is there a downside to doing this:

code:
public boolean compareDouble(double d1 , double d2)
{
       while(d1 <= 1)
        d1 *= 10;
       
       while(d2 <= 1)
        d2 *= 10;
       
       if((int)d1 == (int)d2)
        return true;
       else
        return false;
       
}

Yeah, that's not mathematically correct at all; it won't do what you're looking for... can you see why?

e2: also the initial implementation of the "range" test I put up was kind of flawed, I updated the post for a correct version that was suggested below it.

Look Around You fucked around with this message at 08:28 on Feb 26, 2012

Tots
Sep 3, 2007

:frogout:

Look Around You posted:

I'm guessing you know that's not what you're looking for though, right?

The printf I had will work, but you need to cast your int into a double and divide by 10 again if you want to get the 0.1 etc... you were just passing it 1, 2, 3, [...] which got cast to doubles when passed to printf which is why it came out as 1.0, 2.0 [...]


e:


Yeah, that's not mathematically correct at all; it won't do what you're looking for... can you see why?

e2: also the initial implementation of the "range" test I put up was kind of flawed, I updated the post for a correct version that was suggested below it.
Well I know that it would just drop anything after the first decimal place with a number other than 0. I guess that could end up with either more or less precision than a predefined range

Look Around You
Jan 19, 2009

Tots posted:

Well I know that it would just drop anything after the first decimal place with a number other than 0. I guess that could end up with either more or less precision than a predefined range

What happens if you pass it a negative number?

Tots
Sep 3, 2007

:frogout:

Look Around You posted:

What happens if you pass it a negative number?

Ah, didn't think of that.

Look Around You
Jan 19, 2009

Tots posted:

Ah, didn't think of that.

There's some other major flaws in it too though. Notice anything else?

(I'm not trying to be cryptic I'm just trying to get you thinking about it!)

Tots
Sep 3, 2007

:frogout:

Look Around You posted:

There's some other major flaws in it too though. Notice anything else?

(I'm not trying to be cryptic I'm just trying to get you thinking about it!)

Haha, no I know there is since it doesn't run. I really need to get to sleep though. I have work in the morning. Going to look at it again tomorrow.

Tots
Sep 3, 2007

:frogout:
Oh, no poo poo. Even if the difference is inconceivably small it will evaluate to true.

E: Really going to bed now. Thanks for the late night coding session once again.

oh loving christ and the fact that it doesn't take into account how many powers it removed

I'm going to chalk this up to being tired since they were both terribly obvious.

Tots fucked around with this message at 08:51 on Feb 26, 2012

Look Around You
Jan 19, 2009

Tots posted:

Oh, no poo poo. Even if the difference is inconceivably small it will evaluate to true.

Another thing you need to do is check the scale of it... under your implementation there, 0.0002 should be equal to 0.2 (barring floating point inaccuracy).

Honestly if you want to compare them, abs(a-b) < d where a and b are your test numbers and d is your desired range/precision is probably the best way to go.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
Just to really hammer it home most languages have built-in types specifically for dealing with situations where factions are required but which would go to hell using normal floating point representations, such handling monetary values.

I've no idea what that is in Java as I've never used it, but in C# the relevant type is decimal.

Computer viking
May 30, 2011
Now with less breakage.

rolleyes posted:

Just to really hammer it home most languages have built-in types specifically for dealing with situations where factions are required but which would go to hell using normal floating point representations, such handling monetary values.

I've no idea what that is in Java as I've never used it, but in C# the relevant type is decimal.

As usual, Java isn't very different from C#: It's java.math.BigDecimal.

Henry Black
Jun 27, 2004

If she's not making this face, you're not doing it right.
Fun Shoe
Probably a long shot, but: anyone have experience with Combit's List and Label?

Harvey Mantaco
Mar 6, 2007

Someone please help me find my keys =(
Kind of a simple actionscript question, I'm reading some code here trying to make sense of it and I'm seeing something I don't really get....

if (a || b)
{
Blah Blah Blah
}

A and B are both Integers.

I would think this isn't complete (I would expect something like a || b = c if doing an if statement) but I guess it's a thing in this guys code that does something.... what exactly is it doing?

Edit: Not sure if I should post this here... I haven't seen this before in Java but I'm not sure if this is a common thing in a lot of languages or just specific to ac.

Harvey Mantaco fucked around with this message at 03:48 on Feb 27, 2012

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS
if ((a != 0) || (b != 0))

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Harvey Mantaco posted:

Kind of a simple actionscript question, I'm reading some code here trying to make sense of it and I'm seeing something I don't really get....

if (a || b)
{
Blah Blah Blah
}

A and B are both Integers.

I would think this isn't complete (I would expect something like a || b = c if doing an if statement) but I guess it's a thing in this guys code that does something.... what exactly is it doing?

Edit: Not sure if I should post this here... I haven't seen this before in Java but I'm not sure if this is a common thing in a lot of languages or just specific to ac.

It's implicitly casting a and b to boolean values by sending zero to false and any other value to true. The condition is just checking that at least one of a and b is nonzero.

Harvey Mantaco
Mar 6, 2007

Someone please help me find my keys =(

ultrafilter posted:

It's implicitly casting a and b to boolean values by sending zero to false and any other value to true. The condition is just checking that at least one of a and b is nonzero.

Perfect, thanks both of you.

It's tough as hell to google things like "||", google doesn't seem to include it or something.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

For googling, you're a lot better off using the name "logical or" (remember to turn on verbatim.) That doesn't help when you don't know the name, though (I'm still not sure what to call '->' in PHP.)

raminasi
Jan 25, 2005

a last drink with no ice

delta534 posted:

Make it
code:
add(A,B,Res):- Res is A + B.

Ok, what about
code:
something(A, B, Res) :- Res = [A + B, A - B].
That's either two uses of is or I'm going about this the wrong way.

Strong Sauce
Jul 2, 2003

You know I am not really your father.





carry on then posted:

For googling, you're a lot better off using the name "logical or" (remember to turn on verbatim.) That doesn't help when you don't know the name, though (I'm still not sure what to call '->' in PHP.)

arrow operator.

edit: apparently also object operator.

Strong Sauce fucked around with this message at 05:12 on Feb 27, 2012

delta534
Sep 2, 2011

GrumpyDoctor posted:

Ok, what about
code:
something(A, B, Res) :- Res = [A + B, A - B].
That's either two uses of is or I'm going about this the wrong way.

You will have to use is twice for it to work. For the code you posed the fix is
code:
something(A, B, Res):- T1 is A + B,
                       T2 is A - B,
                       Res=[T1|T2].

tef
May 30, 2004

-> some l-system crap ->
fwiw; plus/3 is a built-in in swi prolog :3:

tef fucked around with this message at 05:47 on Feb 27, 2012

Look Around You
Jan 19, 2009

carry on then posted:

For googling, you're a lot better off using the name "logical or" (remember to turn on verbatim.) That doesn't help when you don't know the name, though (I'm still not sure what to call '->' in PHP.)

Arrow operator. It's the member access operator, but I don't know if they call it that very commonly or anything. (e: I don't know because I don't actually use php because gently caress php)

Look Around You fucked around with this message at 06:05 on Feb 27, 2012

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
Hello dudes and dudettes. This is a pretty mathy question but I know that many of you are good mathematicians as well as programmers.

I'm having trouble figuring out implementation of implicit numerical methods for IVP ODEs. For the simplest case, Backward Euler:

y = y(t)
y' = f(t,y)
y(0) = c

is solved by the time stepping equation

y(n) = y(n-1) + h*f(t(n),y(n)) (1)

(y(n) being the numerically calculated value of y at time t(n) = h*n where h is the step size)

(1) is equivalent to:

g(y(n)) = y(n) - y(n-1) - h*f(t(n),y(n)) = 0 (2)



These equation is implicit, as the thing we're solving for (y(n)) is on both the LHS and RHS, and requires a Fixed Point Iteration to solve. The go-to FPI is Newton's method, which looks like this in the general case, and converges to a root of f:

y(new) = y(previous) - f(y(previous))/f'(y(previous)) (3)

We apply this to g(y(n)), solving iteratively for y(n), giving the equation:

y(n)(new) = y(n)(prev) - g(y(n)(prev))/g'(y(n)(prev)) (4)

But here's my first problem: How are we supposed to know g' ? If our original f(t,y) is arbitrary use input, we don't know g'. So do we have another method in here to numerically estimate g' ?


Second problem is how we set error tolerance on this FPI and also on the numerical estimate of g' (if we do that), given user input accuracy requirements? How does the error tolerance of the NM FPI affect the error rates in the bigger Backward Euler scheme?


Mouthful of a question, I know. Thanks for reading :)

Sir Bobert Fishbone
Jan 16, 2006

Beebort
This is dumb and probably easy, and maybe not programming-related, but I've received several documents I need to configure for word merge from a SQL database. They already include syntax that looks like so:



Is this Word formatting? Can I use this? Would I be better off scrapping and starting anew?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Look Around You posted:

Arrow operator. It's the member access operator, but I don't know if they call it that very commonly or anything. (e: I don't know because I don't actually use php because gently caress php)

Yeah that latter strategy is usually mine but it was the language in the Web Programming class I took. Thanks for the answers.

Look Around You
Jan 19, 2009

carry on then posted:

Yeah that latter strategy is usually mine but it was the language in the Web Programming class I took. Thanks for the answers.

Haha, they teach PHP for a web programming class at my college too; I dropped for medical reasons, but as soon as I saw that it used PHP I was like "oh holy gently caress why". Actually, it started in Java and then moved to PHP :stare:

Anyway, -> is in C/C++ as a shortcut for member access on a pointer to a class/struct*. Perl and PHP use it in general for member access**, presumably because they already use . as string concatenation.

On reading the docs, apparently PHP actually does call -> the object operator. Huh.

* that is p_obj->field is short for (*p_obj).field

** actually, -> means dereferencing and access in perl as well, so $obj->{field} is short for ${$obj}{field}. Member function calls in perl are a bit odd though due to how they do OOP, but basically $obj->meth($arg) is essentially a shortcut for ObjClass::meth($obj, $arg)

Drape Culture
Feb 9, 2010

But it was all right, everything was all right, the struggle was finished. He had won the victory over himself. He loved Big Brother.

The End.

Newf posted:

:words:

Two choices:

since you can calculate f(y) at any point, you can approximate the derivative: df = (f(y+a) - f(y-a))/2a

The other choice is to algebraically differentiate, but that may not be feasible in all cases. (ie not using Matlab). You could also require the derivative formula as an input.

I can't think of how to bound the error in the estimate.

not now
Aug 23, 2008

Newf posted:

numerical methods for IVP ODEs.

As ManlyWeevil said, the first idea is to use finite differences to approximate the derivative. Higher order formulas and error estimates can be found here: http://en.wikipedia.org/wiki/Numerical_differentiation

Is this just for playing around and/or learning how this stuff works? Otherwise it might be useful to look at what GSL / numpy / (a numerical package for whatever language your using) has to offer with regard to ODEs.

Adbot
ADBOT LOVES YOU

Johnny Cache Hit
Oct 17, 2011

Newf posted:

:words:

I can't really answer this because I'm all math stupid (even though I've got a mathematics degree :blush:)

I just wanted to tell you that there is a math question megathread that would probably love your question, if you don't get any other answers here:
http://forums.somethingawful.com/showthread.php?threadid=2347479

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply