Newbie question about type-casts

101 views
Skip to first unread message

Martin Møller Skarbiniks Pedersen

unread,
Aug 2, 2020, 5:09:45 PM8/2/20
to golang-nuts
I have written my first little piece of Go-code.
However it took some time and code for me to divide a int64 by 4 and round down. 

How can the last two lines be rewritten?

Regards
Martin

package main

import (
"fmt"
"bufio"
"math"
)


var cash int64
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
fmt.Println(err)
}

var bet int64
bet = int64(math.Ceil(float64(cash)/float64(4)))
cash = cash - bet



jake...@gmail.com

unread,
Aug 2, 2020, 5:17:29 PM8/2/20
to golang-nuts
There is probably a better way, but lets start with a clear definition of the problem. Your post says you want to "round down" but the variable 'bet' is the result of a divide by 4, rounded up. Which do you actually want to do?

Kurtis Rader

unread,
Aug 2, 2020, 5:25:23 PM8/2/20
to Martin Møller Skarbiniks Pedersen, golang-nuts
In addition to Jake's question regarding whether you want to round up or down I'll point out there isn't any to cast to float64 and back to int64. If you want to "round down" just divide by four. If you want to "round up" add one less than the divisor before dividing; e.g., bet := (cash + 3) / 4. Notice the ":=" which avoids the need for the "var bet int64" statement.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4aa04075-b8bc-41b1-bbb5-d11403e24763o%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Martin Møller Skarbiniks Pedersen

unread,
Aug 4, 2020, 6:31:53 AM8/4/20
to golang-nuts
On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <traxp...@gmail.com> wrote:
I have written my first little piece of Go-code.
However it took some time and code for me to divide a int64 by 4 and round down. 

How can the last two lines be rewritten?

Regards
Martin

package main

import (
"fmt"
"bufio"
"math"
)


var cash int64
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
fmt.Println(err)
}

var bet int64
bet = int64(math.Ceil(float64(cash)/float64(4)))
cash = cash - bet

On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
In addition to Jake's question regarding whether you want to round up or down I'll point out there isn't any to cast to float64 and back to int64. If you want to "round down" just divide by four. If you want to "round up" add one less than the divisor before dividing; e.g., bet := (cash + 3) / 4. Notice the ":=" which avoids the need for the "var bet int64" statement.

Oops. I actually needed to round up so I think I still need the math.Ceil() function.

If I write like this:

<code>
import (
"fmt"
"bufio"
"math"
)

var cash int64
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
fmt.Println(err)
}

bet := math.Ceil(cash/4)
</code>
then the compiler complains:
./casino.go:36:24: cannot use cash / 4 (type int64) as type float64 in argument to math.Ceil

My only solution is a lot of type-casting:
bet := int64(math.Ceil(float64(cash)/float64(4)))

Is there a better way to divide by 4 and round up to nearest integer?

ps. The full code can be found at: https://pastebin.com/1j7NJF0k


Regards
Martin



Martin Møller Skarbiniks Pedersen

unread,
Aug 4, 2020, 6:37:06 AM8/4/20
to golang-nuts
On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <traxp...@gmail.com> wrote:
I have written my first little piece of Go-code.
However it took some time and code for me to divide a int64 by 4 and round down. 

[...]
var bet int64
bet = int64(math.Ceil(float64(cash)/float64(4)))

On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
In addition to Jake's question regarding whether you want to round up or down I'll point out there isn't any to cast to float64 and back to int64. If you want to "round down" just divide by four. If you want to "round up" add one less than the divisor before dividing; e.g., bet := (cash + 3) / 4. Notice the ":=" which avoids the need for the "var bet int64" statement.


OK. Good idea. But what is I need to integer divide with a variable and not the number 4?

Cheers
Martin

 

Michael Jones

unread,
Aug 4, 2020, 9:46:31 AM8/4/20
to Martin Møller Skarbiniks Pedersen, golang-nuts
var cash, change, bet int64
:
bet = cash/4

// change is 0,1,2,3 meaning 0/4, 1/4, 2/4, 3/4 dineros or zlottys or whatever
change = cash - 4*bet // or, change = cash%4

// choose one of these 
if change>0{
  bet++ // a: round up means 1/4 and 2/4 and 3/4 go to 1
}
// ...or...
if change>=2{
  bet++ // b: round up means 2/4 and 3/4 go to 1
}

cash = cash - bet

once you understand the above compare to these versions:

bet = (cash+3)/4 // this is Kurtis's advice, mode (a)
cash = cash - bet
...or...
bet = (cash+2)/4 // this is mode (b)
cash = cash - bet
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.


--
Michael T. Jones
michae...@gmail.com

Jesper Louis Andersen

unread,
Aug 4, 2020, 9:50:21 AM8/4/20
to Martin Møller Skarbiniks Pedersen, golang-nuts
On Tue, Aug 4, 2020 at 12:31 PM Martin Møller Skarbiniks Pedersen <traxp...@gmail.com> wrote:

bet := int64(math.Ceil(float64(cash)/float64(4)))


Programming languages must make a conscious choice here. Do we make type casts explicit or implicit? Go opted for the explicit approach, probably based on the experience of C, where type promotion follows certain implicit rules.

The advantage of an explicit approach is that you remove certain classes of subtle errors from programs, in a trade-off for more typing. You can arrange your values such that they already have the correct type at times, but otherwise you have to make the conversion yourself explicitly in the program. 

-- 
J.

peterGo

unread,
Aug 4, 2020, 10:07:12 AM8/4/20
to golang-nuts
Martin Møller Skarbiniks Pedersen,

Use a RoundUp function for n / d. For example,

// n / d rounded up
func RoundUp(n, d int64) int64 {
    if d == 0 {
        return 0
    }
    return (n + (d - 1)) / d
}



Peter
Reply all
Reply to author
Forward
0 new messages