2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Golangで累乗のプログラムを実装したときに、いろいろ勉強になった事項があったので記事にしました

プログラミング課題

以下のプログラミングの問題を考えます

整数 A = 202、B = 134、C = 107 とします。(A + B) * C の2乗を計算した結果を出力してください。

最初に考えた実装例

最初に実装したのは以下のソースコードです

package main
import  ( "fmt"
          "math"
        )
        
func main(){
       
    a := 202
    b := 134
    c := 107
    ans := math.Pow((a + b ) * c , 2) 
    fmt.Println(ans
}

上の例ではans変数のデータ型がfloat型になります。

次に考えた実装例

次に考えた実装例は以下の通りです

package main
import "fmt"
//import "math"
func main(){
        
    a := 202
    b := 134
    c := 107
    ans := (a+b)*c
    fmt.Println(ans * ans)
}

こちらの例ではansが整数型となります

詳細

mathオブジェクトのPowメソッドを使って2乗を計算すると、返り値がfloat型になります。

最後に

Goはデータ型が厳格ですね。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?