Questions tagged [off-by-one]

A class of errors commonly made by programmers characterized by accessing arrays at an index, whose value is greater or lesser than the intended value by 1

An off-by-one error (OBOE) is a logic error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many or too few. Usually this problem arises when a programmer fails to take into account that a sequence starts at zero rather than one (as with array indices in many languages), or makes mistakes such as using "is less than or equal to" where "is less than" should have been used in a comparison. This can also occur in a mathematical context.

[source]

50 questions
18
votes
2 answers

LocalDate.plus Incorrect Answer

Java's LocalDate API seems to be giving the incorrect answer when calling plus(...) with a long Period, where I'm getting an off by one error. Am I doing something wrong here? import java.time.LocalDate; import java.time.Month; import…
Daniel Centore
  • 2,968
  • 1
  • 15
  • 35
6
votes
1 answer

Why is my video memory offset calculation off by one?

I have been reading and following a tutorial on writing an operating system from scratch by Nick Blundell which can be found at https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf I have successfully written a boot loader that…
Jakeman582
  • 91
  • 5
3
votes
1 answer

TASM addressing off by one

I'm currently implementing Snake for university, and we have to use TASM for that. My main game data is laid out like this (using C syntax): struct GameLine { uint8_t direction_bits[10]; // 2 bits per entry, the first entry is invalid since…
CodenameLambda
  • 1,404
  • 9
  • 23
3
votes
1 answer

Visible rows in DataGrid is off by 1 (counted using ContainerFromItem)

I have a DataGrid of variable dimensions dependent upon screen-res. I need to know how many rows are visible to the user. Here's my code: uint VisibleRows = 0; var TicketGrid = (DataGrid) MyWindow.FindName("TicketGrid"); foreach(var Item in…
Danny Beckett
  • 18,294
  • 21
  • 100
  • 129
3
votes
0 answers

How to set form field attributes on a django inline form

So i'm trying to disable some form field widgets in an inline form (by setting disabled attribute in rendered html). The following is a minified version of some code that seems to half work: class IncomingItemForm(forms.ModelForm): class Meta: …
Khodeir
  • 375
  • 2
  • 13
3
votes
3 answers

CEIL is one too high for exact integer divisions

This morning I lost a bunch of files, but because the volume they were one was both internally and externally defragmented, all of the information necessary for a 100% recovery is available; I just need to fill in the FAT where required. I wrote a…
Synetech
  • 8,907
  • 7
  • 59
  • 90
2
votes
1 answer

Is there a bug in Algorithm 4.3.1D of Knuth's TAOCP?

You can find the explanation of Algorithm 4.3.1D, as it appears in the book Art of The Computer Programming Vol. 2 (pages 272-273) by D. Knuth in the appendix of this question. It appears that, in the step D.6, qhat is expected to be off by one at…
yasar
  • 11,262
  • 26
  • 80
  • 154
2
votes
2 answers

Python loop iterates after expected termination

The below Python code has an odd behavior, which I can't figure out. The program calls testQuery, which asks the used to reply 'yes' to call scoreAverager, or 'no' to exit the program. If scoreAverager is called, it then requests the user enter a…
2
votes
1 answer

Why isn't my DP solution to decode numbers and letters working for all test cases?

Context This problem was asked by dailycodingproblem and leetcode /* Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded. For example, the message '111' would give 3, since it could be…
2
votes
1 answer

Python weirdness involving map and reduce

Sorry for the vague title, but I really have no idea what's going on here. from functools import reduce arr = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] def strxo(n): if (n == -1): return "X" elif (n == 1): return "O" else: …
2
votes
1 answer

EPPlus Off-by-One Fix

I'm updating some old code, and want to replace some code to create excel documents with EPPlus. The problem is my old code indexes things from [0, n-1] and EPPlus indexes from [1, n]. Is there any way to account for this, other than to manually…
Zackary
  • 175
  • 9
2
votes
2 answers

JLabel text is garbled

I'm seeing a strange issue involving a JLabel that is used to display a range of numbers. The text of the label is typically something like 0.0 - 100.0 (for example). The problem is that for a select few users, the text appears garbled. In this…
Jacob Wallace
  • 700
  • 2
  • 10
  • 22
1
vote
0 answers

How do you spot or avoid committing off-by-one errors?

When I write code, among the more common classes of bug I create is off-by-one errors (OBO). In "real" code I most often run into this issue while doing complicated pointer/iterator arithmetic in C or C++, but I've also at times had issues with it…
Ray Hamel
  • 1,129
  • 4
  • 15
1
vote
0 answers

Trouble updating a batch of cells in Google Sheets App Scripts

So, I am running a program that sorts through a large spreadsheet and organizes each row based into other sheets based on properties of individual cells in that row. I got the macro working, but it was extremely slow so I went back and started from…
1
vote
2 answers

Avoiding Python Off-by-One Error in RLE Algorithm

EDIT: there's more wrong with this than just an off-by-one error, it seems. I've got an off-by-one error in the following simple algorithm which is supposed to display the count of letters in a string, along the lines of run-length encoding. I can…
Robin Andrews
  • 2,407
  • 4
  • 27
  • 62
1
2 3 4