{"record":{"id":"b2ee8e09b832a1a3","repo":"TheAlgorithms/Go","slug":"integer-must-have-ve-value","errorCode":null,"errorMessage":"integer must have +ve value","messagePattern":"integer must have \\+ve value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/decimaltobinary.go","lineNumber":36,"sourceCode":"\t\"errors\"\n\t\"strconv\"\n)\n\n// Reverse() function that will take string,\n// and returns the reverse of that string.\nfunc Reverse(str string) string {\n\trStr := []rune(str)\n\tfor i, j := 0, len(rStr)-1; i < len(rStr)/2; i, j = i+1, j-1 {\n\t\trStr[i], rStr[j] = rStr[j], rStr[i]\n\t}\n\treturn string(rStr)\n}\n\n// DecimalToBinary() function that will take Decimal number as int,\n// and return its Binary equivalent as a string.\nfunc DecimalToBinary(num int) (string, error) {\n\tif num < 0 {\n\t\treturn \"\", errors.New(\"integer must have +ve value\")\n\t}\n\tif num == 0 {\n\t\treturn \"0\", nil\n\t}\n\tvar result string = \"\"\n\tfor num > 0 {\n\t\tresult += strconv.Itoa(num & 1)\n\t\tnum >>= 1\n\t}\n\treturn Reverse(result), nil\n}\n","sourceCodeStart":18,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/decimaltobinary.go#L18-L48","documentation":"DecimalToBinary only supports non-negative integers because the repeated division-by-2 loop produces an infinite/negative loop for negative input. It returns this ad-hoc error when num < 0. num == 0 is handled specially and returns \"0\".","triggerScenarios":"DecimalToBinary(-1) or any negative int, e.g. converting a signed value that went negative due to subtraction or a signed-byte cast.","commonSituations":"Converting temperature deltas, signed sensor readings, or results of integer underflow (e.g. 0 - 1 on unsigned-like logic) into binary.","solutions":["Check num >= 0 before calling; reject or take math.Abs if magnitude is intended.","For negative numbers, convert the magnitude and prefix '-' manually, or use strconv.FormatInt(int64(num), 2) which handles the sign.","Fix upstream arithmetic that unexpectedly produces negative values."],"exampleFix":"// before\nb, err := conversion.DecimalToBinary(delta) // errors when delta < 0\n// after\nif delta >= 0 {\n    b, err = conversion.DecimalToBinary(delta)\n} else {\n    b = \"-\" + strconv.FormatInt(int64(-delta), 2)\n}","handlingStrategy":"validation","validationCode":"func isNonNegative(n int) bool { return n >= 0 }","typeGuard":null,"tryCatchPattern":"b, err := conversion.DecimalToBinary(num)\nif err != nil {\n    if err.Error() == \"integer must have +ve value\" {\n        return fmt.Errorf(\"cannot convert negative number %d\", num)\n    }\n    return err\n}","preventionTips":["Assert num >= 0 before conversion.","Handle signed values via strconv.FormatInt which emits a '-' prefix.","Audit arithmetic upstream for possible underflow."],"tags":["conversion","binary","go","negative-number"],"backgroundTag":"negative-number-unsupported","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}