strange 3 digit double
to generate numbers to multiply i made this:
int f = (int)(Math.random() * 1000);
int k = (int)(Math.random() * 3);
double parcela = f * Math.pow(10, -k);
return parcela;
and got this:
25.3 x 7.930000000000001
how's it possible?
do i have to format it?
[nobr]I wrote this class file recently for precisely this kind of problem/**
*-description :
Sets the number of decimal places to the argument specified
*the class is overloaded to return float and double data types. The parameter
*named 'places' is the number of decimal places returned by the method.
*Note: the parameter int will also work (without amendment) for byte and short.
*// <b>EXAMPLE:</b><br>
*<code>
* double d = 1.250550123456789;
* System.out.print( Fraction.digits(d,4) );
*// will output the double as : 1.2506
* float f = (float)1.2505012;
* System.out.print( Fraction.digits(f,3) );
*// will output the double as : 1.251
*</code>
*This has the advantage over other methods in that the data type retains its
*integrity as a double or a float data-type, instead of converting it to a String
*as in java.text.DecimalFormat and rounding off the additional data.<br>
*The class could be also used to avoid the pitfalls of floating point
*innacuracies;<br>
*// <b>EXAMPLE:</b><br>
*<code>
* double d1 = 3.0 * 0.1;
* System.out.println(d1*10);
*// will output : 3.0000000000000004
* double d2 = 3.0 * 0.1;
* byte b = (byte)14; // works with other data type widths
* System.out.println(Fraction.digits(d2*10, b)); // slice off the last digit
*// will output : 3.0
*</code>
* @author Sum-shusSue
*/
public class Fraction{
public static double digits(double d, int places){
return (long)(d*Math.pow(10,places)+0.5)/Math.pow(10,places);
}
public static double digits(double d, long places){
return (long)(d*Math.pow(10,places)+0.5)/Math.pow(10,places);
}
public static float digits(float d, int places){
return (int)(d*Math.pow(10,places)+0.5)/(float)Math.pow(10,places);
}
public static float digits(float d, long places){
return (int)(d*Math.pow(10,places)+0.5)/(float)Math.pow(10,places);
}
}
[/nobr]
t tried your class and got this again: 55.900000000000006
here's my code:
int f = (int)(Math.random() * 1000);
int k = (int)(Math.random() * 3);
double parcela = f * Math.pow(10, -k);
Fraction fr = new Fraction();
byte b = (byte)14;
fr.digits(parcela, 14);
return parcela;
can you help me pls?
thanks in advance
13 digits will always return a true result
i used 13 and got this: 0.8200000000000001
public double getParcela () {
int f = (int)(Math.random() * 1000);
int k = (int)(Math.random() * 3);
double parcela = f * Math.pow(10, -k);
Fraction fr = new Fraction();
byte b = (byte)13;
fr.digits(parcela, b);
return parcela;
return Fraction.digits(parcela, 13);// You are returning the original double not the ammended double
lolstupid of methanks!BTW, the site you refer to in your info, is it yours?
> lol> stupid of me> thanks!> > BTW, the site you refer to in your info, is it yours?Yarr.
> looks cool!Thank you! (I give away my secrets with this: surname and profession etc)
> lol> stupid of me> thanks!> It happens, we all get tripped and then feel a bit silly by the simplest of things IMO, very easy to do - worry thee not.
i'm sorry to use this tread, but i still cant get how to ge scite to work: i downloaded the zip, and cant find an installer or so;
so i've to build it myself!
am i wrong?
note:
some time ago i bought my first java book and also had a guess number, but with around 100 lines of code!!!
hehe
Just create your own folder, stick all the bits in there and click on the golf ball thingy
> some time ago i bought my first java book and also had
> a guess number, but with around 100 lines of code!!!
There's a deliberate logic error in that code (GuessMyNumber.java) for the budding student to fathom and correct. I cannot imagine how it's possible for 100 lines of code could be useful for a newbie - simplicity is important but 100 lines?!? (strikes me as insanity)
of course this isnt one of my days: only later on i discovered there were several windows installer downloads available
here's the original code
yes, from a new on java book
/*
* Adivinha.java
*
* Created on 14 de Setembro de 2002, 15:51
*/
/**
*
* @author Pedro Coelho
* @version
*/
import java.lang.Math;
public class Adivinha {
public static void main (String args[])
throws java.io.IOException
{
boolean sair = false;
quebrar:
do
{/* Ciclo Principal */
boolean guess = false;
char c;
int tentativas = 0;
/* Gerar o numero aleatorio entre 0 e 100 */
double d = Math.random();
int num = (int) (100 * d);
while (guess == false)/* Ciclo Interno */
{
/* Pedir o numero ao utilizador */
String s = "";
System.out.println("Qual e o numero? (0-100, -1 para desistir)");
while ((c = (char) System.in.read()) != 10)
s += c;
s = s.substring(0,s.length()-1); // Eliminacao de lixo no final da String
int n = Integer.valueOf(s).intValue();
tentativas ++;/* Incrementar o contador de tentativas */
/* Testar a resposta do utilizador */
if (n == -1)
{
System.out.println("Voc? desistiu!");
break quebrar;
}
if (n == num)
guess = true;
else
if (n > num)
System.out.println(n + "? N?o, o n?mero e mais baixo!");
else
System.out.println(n + "? N?o, o n?mero e mais alto!");
} /* Fim do ciclo interno */
/* Notificar o utilizador da vitoria */
System.out.println("Voce adivinhou o numero em " + tentativas + " tentativas");
/* Perguntar se quer jogar de novo */
boolean repetir; /* Variavel que permite a saida do ciclo */
do {
System.out.println("Quer jogar de novo? (S/N)");
c = (char) System.in.read();
switch (c) {
case 's':
case 'S': System.out.println("Vamos jogar outra vez!");
repetir = false;
break;
case 'n':
case 'N': System.out.println("Ate a proxima...");
repetir = false;
sair = true;
break;
default: System.out.println("A sua resposta nao e valida");
repetir = true;
break;
} /* Fim do switch */
}
while (repetir == true); /* So saimos deste ciclo
quando houver uma resposta
valida S ou N */
System.in.read();
System.in.read(); // Ler o RETURN da Resposta
}
while (sair == false);// Condicao de saida do ciclo global
}
}
i dont want to abuse on your patience, so can you pls tell me how i activate code completion in Scite?
i tried in global actions, but it didnt work...
yhanks in advance
Hmm, well apart from the Portguese;-
1. I don't think do-while is a particularly elegant data structure. I cannot speak for all but I hardly ever use it and cannot recall the last time I implemented a do-while
2. I do not like the switch flag condition inside the loop at all
3. It just seems generally ugly to me (I won't bother to have it translated it for my own newbies)
As for getting scite to perform, do you have this in your autoexec.bat file
SET CLASSPATH=.;%CLASSPATH%
i just showed it to you as an example of bad coding - and that from an author...
amazing!
well
i've winxp, so no autoexe.bat
but in my classpath i'ce
C:\j2sdk1.4.2_03/bin
is that enough?
and i'd like, like in GEL, code completion to my own classes too...
On XP it should be enough, yes.What messages do you get when you try to compile a *.java file?(PS: I'm surprised you haven't burned that book yet, I'd hate to see the author's code on a bad day)
well, i saved my test file in chere's the error msg:>javac Teste.java>Exit code: 0>java Testejava.lang.NoClassDefFoundError: TesteException in thread "main" >Exit code: 1note: still no code completion...
Use Math.floor() for those kinds of problems.
tx 4 the idea, but that's not what i wantMath.floor(3.650000000000004) gives 3.0 and i want 3.65
> Use Math.floor() for those kinds of problems.This is nonsense
> well, i saved my test file in c
> here's the error msg:
> >javac Teste.java
> >Exit code: 0
> >java Teste
> java.lang.NoClassDefFoundError: Teste
> Exception in thread "main" >Exit code: 1
>
> note: still no code completion...
You have a classpath issue, thats all I think.
i just added C:\ (where i saved the test file) and run okbut as to code completion... nothingdo i have to set anything special?
class Myclass {public String Mymethod() {//...}}when you type Myclass mc = new Myclass();mcDOT...then the available list of methods pops up
No such number with scite
ahhbut GEL does have iti know i'm very very new to java, so forgive me telling you this: give it a look - seems greatanyway thanks a lot from your overall help
> ahh
> but GEL does have it
> i know i'm very very new to java, so forgive me
> telling you this: give it a look - seems great
>
All these threads on what is the best IDE, it seems to me that many people promote what they leant first (proof:- there's no way anyone should be able to make a claim that TextPad, JEdit or even NetBeans is a 'good' IDE). These things are open to subjectivity of course too, so what do I know?
I will take a peek at your Gel all the same.
> anyway thanks a lot from your overall help
I think Fraction.java is jolly useful and I shall continue to post it, irrespective of whether newbies understand the the extensive, or even (arguably) excessive javadoc formatting and as for your GuessMyNumber program, it gives hope for us all..
>There's a deliberate logic error in that code
if one enters without typing or not typping a number, gets a:
java.lang.NumberFormatException: For input string: ""
try {
myGuess = Integer.parseInt(str);
}
catch (NumberFormatException nfe) {
//Tell teacher its fixed!
}
I'll give you a C- for that (take a closer look at the while condition)
:( for the c- - well, i didnt hope for a c++ lolanyway you didnt say there were several errors, so i just found one and posted, but you'r rightwhile(myGuess != rand) {