Video nummer 4

Her kan du diskutere opgaverne til Java serien og evt. dele dine egne løsningsforslag med andre.
Besvar
Henrik Halle
Entusiast
Entusiast
Indlæg:38
Tilmeldt:4. maj 2014, 09:22
Video nummer 4

Indlæg af Henrik Halle » 17. jan 2015, 15:27

Her er et løsningsforslag til opgaverne i video 4 i javaserien.
Jeg kan nu kun få det til 5 variabler og ikke 6 som videoen taler om @ 18:14

Kode: Vælg alt

package kap04;

public class Main {

	public static void main(String[] args) {
		

		/* Operationer med heltal og int variabler 
		 * 
		 */
		
		int a = 20;
		int tal = 5;
		
		
		int b = a + tal;
		int c = a - tal;
		int d = a * tal;
		int e = a / tal;
		int f = a % tal;
		
		System.out.println("Plus " + a + " + " + tal + " = " + b);
		System.out.println("Minus " + a + " - " + tal + " = " + c);
		System.out.println("Gange " + a + " * " + tal + " = " + d);
		System.out.println("Division (heltal) = " + a + " / " + tal + " = " + e);
		System.out.println("Modelus = " + a + " % " + tal + " = " + f);
	
	/* Operationer med kommaatal og doubles 
	 * 
	 */
		double aa = 20.0;
		double taltal = 5.3;
		
		
		double bb = a + taltal;
		double cc = a - taltal;
		double dd = a * taltal;
		double ee = a / taltal;
		double ff = a % taltal;
		
		System.out.println("Plus " + aa + " + " + taltal + " = " + bb);
		System.out.println("Minus " + aa + " - " + taltal + " = " + cc);
		System.out.println("Gange " + aa + " * " + taltal + " = " + dd);
		System.out.println("Division (kommatal) = " + aa + " / " + taltal + " = " + ee);
		System.out.println("Modelus kan man ikke med doubles ");
	
	
	}
	

}

Kode: Vælg alt

Minus 20 - 5 = 15
Gange 20 * 5 = 100
Division (heltal) = 20 / 5 = 4
Modelus = 20 % 5 = 0
Plus 20.0 + 5.3 = 25.3
Minus 20.0 - 5.3 = 14.7
Gange 20.0 * 5.3 = 106.0
Division (kommatal) = 20.0 / 5.3 = 3.7735849056603774
Modelus kan man ikke med doubles 

cristian
Moderator
Moderator
Indlæg:882
Tilmeldt:26. sep 2011, 21:31
Kontakt:

Re: Video nummer 4

Indlæg af cristian » 17. jan 2015, 18:58

Vi må få Jeppe på banen med den sjette variable, da jeg ikke lige ved hvad den skulle være heller.

Jeg kunne ikke acceptere teksten
Husk modulus ikke virker på double
Straks tænkte jeg... Hvorfor? :D

Åbenbart har det noget at gøre med den standard sat frem af IEEE nummer 754, og den måde Java udregner numeriske værdier (FPU).
Hvor nogle værdier i FPU er umulige at vise præcist i IEEE's standard. Så Java runder værdien til at "passe".

For single og double præcisions modulus, virker modulus også anderledes og udregner ikke rest.

Kode: Vælg alt

Double d1 = new Double(5.0d);
Double d2 = new Double(1.3d);
		
System.out.println(d1 % d2);
1.0999999999999999
% is also defined to work with float and double operands, though that use is quite rare.

The result of a floating-point remainder operation as computed by the % operator is not the same as that produced by the remainder operation defined by the IEEE 754 floating point standard. The IEEE (Institute of Electrical & Electronics Engineers) 754 remainder operation computes the remainder from a rounding division, not a truncating division. % on floating-point operations behaves analogously to the integer remainder operator; this may be compared with the C library function fmod. The IEEE 754 remainder operation may be computed by the library routine Math. IEEEremainder. (Note the violation of the naming conventions.)
Herfra bliver det super kryptisk super hurtigt, og der er ikke rigtig nogle solide forklaringer almindelige mennesker kan forstå.
The result of a floating-point remainder operation is determined by the rules of IEEE arithmetic:

If either operand is NaN, the result is NaN.
If the result is not NaN, the sign of the result equals the sign of the dividend.
If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
If the dividend is finite and the divisor is an infinity, the result equals the dividend.
If the dividend is a zero and the divisor is finite, the result equals the dividend.
In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from the division of a dividend n by a divisor d is defined by the mathematical relation r=n-(d·q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d.
For at udregne single eller double præcisions værdier, kan man bruge BigDecimal Java klassen

Kode: Vælg alt

BigDecimal bd1, bd2, bd3;
		
bd1 = new BigDecimal("11.254");
bd2 = new BigDecimal("-8.321");
bd3 = bd1.remainder(bd2);
		
System.out.println("Value of bd3 is " + bd3);
Value of bd3 is 2.933

jepperask
Geni
Geni
Indlæg:378
Tilmeldt:4. nov 2012, 17:57

Re: Video nummer 4

Indlæg af jepperask » 17. jan 2015, 22:20

Mon ikke han mener at i først danner jeres int variabel, og derefter de 5 variabler (en for hver operatør) og derfor i alt 6 - ellers har han måske bare skrevet forkert.

Besvar