Die letzten 6 Beiträge |
Grisu |
Super, hat geklapptz, Danke schön |
eulerscheZahl |
Ich habe es jetzt nicht getestet, aber versuche mal:
temp = (float)1 / b;
edit: temp = 1.0f / b; sollte es auch tun |
Grisu |
Also in c++ klappt es, in Java sagt mir der compiler, dass er eine definition als double haben möchte und dass ihm float nicht ausreicht.
Meldung ist: possible loss of precision
found: double
required: float
Wo ist da das Problem?
Danke noch einmal. |
Grisu |
Super, danke schön, ich werde gleich mal schauen, obs dann klappt.
Vielen Dank. |
eulerscheZahl |
float temp = 1.0 / b;
sonst wird die Division mit int durchgeführt, da kommt 0 raus. |
Grisu |
java, c++ Programm läuft nicht (negative Potenzen)
Hi @all,
ich hab nen kleines Problem, das ich mir nicht erklären kann, es läuft wder in Java noch in C++.
Ich möchte die negativen Potenzen berechnen ohne Verwendung mathematischer Funktionen wie z.B. Math.pow in Java.
Ich schreibe einmal, wie ich es lösen wollte:
Java:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
|
public class Potenzen{
public static float funktion(int b, int x){
float p=1;
float temp=1/b;
x=x*(-1);
for(i=1;i<=x;i++){
p=p*temp;
}
return p;
}
public static void main(String args[]){
system.out.println(funktion(2,-2));
}
}
|
|
C++:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
|
#include<iostream>
using namespace std;
float potenz(int,int);
float potenz(int b,int x){
float p=1;
float t=1/b;
x=x*(-1);
for(int i=1;i<=x;i++){
p=p*t;
}
return p;
}
int main(){
cout<<potenz(2,-2);
return 0;
}
|
|
Ich hab erwartet, dass mir 0,25 ausgegeben wird, ist aber nicht so, es wird ganz stumpf 0 ausgegeben, egal, welche Basis und welchen Exponenten ich wähle.
Warum? |
|
|