package main
import (
"fmt"
"math"
)
func main() {
// 係数a, b, cを定義
a := 4.0
b := 24.0 // float64型に統一
c := 12.0 // float64型に統一
// 元の二次方程式を表示
fmt.Printf("%.1f(x^2)+%.1f(x^1)+%.1f(x^0)=0\n", a, b, c)
// 解の公式の判別式を計算
discriminant := b*b - 4*a*c
fmt.Println("判別式:", discriminant)
// 判別式の値によって解を場合分け
if discriminant > 0 {
// 判別式が正の場合、2つの異なる実数解を持つ
x1 := (-b + math.Sqrt(discriminant)) / (2 * a)
x2 := (-b - math.Sqrt(discriminant)) / (2 * a)
fmt.Printf("2つの異なる実数解があります: x1=%.4f, x2=%.4f\n", x1, x2)
} else if discriminant == 0 {
// 判別式が0の場合、重解を持つ
x := -b / (2 * a)
fmt.Printf("1つの実数解(重解)があります: x=%.4f\n", x)
} else {
// 判別式が負の場合、2つの異なる虚数解を持つ
realPart := -b / (2 * a)
imaginaryPart := math.Sqrt(-discriminant) / (2 * a)
fmt.Printf("2つの異なる虚数解があります: x1=%.4f+%.4fi, x2=%.4f-%.4fi\n", realPart, imaginaryPart, realPart, imaginaryPart)
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: