标签: off-by-one

LocalDate.plus错误答案

Java的LocalDate API似乎在plus(...)用long 调用时给出了错误的答案Period,在这种情况下我遇到一个错误。我在这里做错什么了吗?

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class Main
{
    public static void main(String[] args)
    {
        // Long Period
        LocalDate birthA = LocalDate.of(1965, Month.SEPTEMBER, 27);
        LocalDate eventA = LocalDate.of(1992, Month.MAY, 9);
        LocalDate halfA = eventA.plus(Period.between(birthA, eventA));
        System.out.println(halfA); // 2018-12-21 ????
        System.out.println(ChronoUnit.DAYS.between(birthA, eventA)); // 9721
        System.out.println(ChronoUnit.DAYS.between(eventA, halfA)); // 9722 ????

        // Short Period
        LocalDate birthB = LocalDate.of(2012, Month.SEPTEMBER, 10);
        LocalDate eventB = LocalDate.of(2012, Month.SEPTEMBER, 12);
        LocalDate halfB = eventB.plus(Period.between(birthB, eventB));
        System.out.println(halfB); // …
Run Code Online (Sandbox Code Playgroud)

java java-8 off-by-one localdate

18
推荐指数
2
解决办法
355
查看次数

为什么我的视频内存偏移量计算会减一?

我一直在阅读并遵循Nick Blundell从零开始编写操作系统的教程,可以在https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-找到dev.pdf

我已经成功编写了一个可以调用C代码的引导加载程序,因此我开始在C中编写内核.我现在正在尝试编写可以在屏幕上打印字符和字符串的函数.当我开始执行C代码时,我处于32位保护模式,所以我试图正确计算视频内存地址0xb8000的内存偏移量.

当我尝试使用计算的偏移量访问视频内存的特定区域时,我的问题就出现了.由于文本区域是25行乘80列,我使用公式((行*80)+列)*2,因为我必须有一个字符字节和一个属性字节.当我设置row = 0和column = 0时,我正在尝试打印的X不存在.设置row = 0和column = 1,X出现在左上角.

从char*video_memory = 0xb8000开始并重复发出video_memory ++允许我正确访问每个字节并在黑色背景上打印空格.

这是我的主要代码:

#include "../drivers/screen.h"

void main() {

   //clear_screen();
   //print_character('X', 0, 0, 0);

   // Helper variables.
   int row;
   int column;

   // We need to point at 0xB8000, where video memory resides.
   unsigned char* video_memory = (unsigned char*)0xB8000;
   for(row = 0; row < 25; row++) {
      for(column = 0; column < 80; column++) {
         // Clear the screen by printing a space on a …
Run Code Online (Sandbox Code Playgroud)

c operating-system video-memory off-by-one

6
推荐指数
1
解决办法
108
查看次数

对于精确的整数除法,CEIL太高了

今天早上我丢失了一堆文件,但因为它们的内容和外部碎片整理,所以100%恢复所需的所有信息都可用; 我只需要在必要时填写FAT.

我编写了一个程序来执行此操作并在我转储到文件的FAT副本上对其进行测试,除了对于一些文件(526中的17个)之外,它的工作方式完美,FAT链是一个单独的集群太长,从而与下一个文件交叉链接.

幸运的是,我确切地知道问题是什么.我ceil在EOF计算中使用过,因为即使单个字节结束也需要一个额外的集群:

//Cluster    is the starting cluster of the file
//Size       is the size (in bytes) of the file
//BPC        is the number of bytes per cluster
//NumClust   is the number of clusters in the file
//EOF        is the last cluster of the file’s FAT chain

DWORD NumClust = ceil( (float)(Size / BPC) )
DWORD EOF      = Cluster + NumClust;
Run Code Online (Sandbox Code Playgroud)

此算法适用于除大小恰好是簇大小的倍数的文件之外的所有内容,在这种情况下,它们最终成为一个群集太多.

我考虑了一段时间但是对于这样做的方法感到茫然.看起来它应该很简单,但不知何故,这是非常棘手的.

什么公式适用于任何大小的文件?

c++ formula ceil off-by-one

3
推荐指数
1
解决办法
369
查看次数

DataGrid中的可见行偏差为1(使用ContainerFromItem计算)

我有一个DataGrid可变维度依赖于screen-res.我需要知道用户可以看到多少行.这是我的代码:

uint VisibleRows = 0;
var TicketGrid = (DataGrid) MyWindow.FindName("TicketGrid");

foreach(var Item in TicketGrid.Items) {
    var Row = (DataGridRow) TicketGrid.ItemContainerGenerator.ContainerFromItem(Item);
    if(Row != null && Row.IsVisible) {
        VisibleRows++;
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码来测试变量:

MessageBox.Show(String.Format("{0} of {1} rows visible", VisibleRows, TicketGrid.Items.Count));
Run Code Online (Sandbox Code Playgroud)
  • 当网格中没有行时,它会正确显示0行中的0个可见:

  • 当网格中有1行时,它会正确显示1行中的1行:

  • 当网格中有9行时,它会正确显示9行中的9行:

  • 下一行是"半可见",所以我会指出它显示10行中有10行可见:

  • 但是,要添加的下一行显然是可见的,错误地显示11行中的11个可见:

  • 在此之后添加的行是正确的(禁止杂散1),例如18行中的11个可见:


我不能只是- 1,因为在添加了一定数量之后它才是不正确的.我无法检查> 10,因为尺寸是可变的.

我怎样才能解决这个问题?

.net c# wpf datagrid off-by-one

3
推荐指数
1
解决办法
512
查看次数

TASM 寻址减一

我目前正在为大学实施 Snake,为此我们必须使用 TASM。

我的主要游戏数据是这样排列的(使用 C 语法):

struct GameLine {
    uint8_t direction_bits[10];  // 2 bits per entry, the first entry is invalid since valid x positions start at one
    uint8_t collision_bits[5];  // 1 bit per entry, the first entry is again invalid but has to stay zero
    uint8_t aux;  // padding such that GameLine is 16 bytes big, also used to store other information at times.
};

struct GameData {
    struct GameLine lines[23];
} PHYSICAL_GAME_DATA;
Run Code Online (Sandbox Code Playgroud)

问题是每帧写入方向位会覆盖大 x 位置的读取冲突位(38 是最大位置,但它发生得更早)。我说“读取冲突位”是因为我无法验证physical_game_data实际驻留的位置,因为我不知道如何指示汇编器 …

memory assembly tasm off-by-one x86-16

3
推荐指数
1
解决办法
48
查看次数

Python怪异涉及map和reduce

对于模糊的标题很抱歉,但我真的不知道这里发生了什么.

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:
        return "_"

def prboard(board):
    print(reduce(lambda x, y: x + "\n" + y, list(map(lambda l: reduce(lambda a, b: strxo(a) + strxo(b), l), board))))

prboard(arr)
Run Code Online (Sandbox Code Playgroud)

期望的输出:

___
___
___
Run Code Online (Sandbox Code Playgroud)

实际输出:

__
__
__
Run Code Online (Sandbox Code Playgroud)

当我改变决赛elsestrxo,return str(n)而不是return "_"我得到:

000
000
000
Run Code Online (Sandbox Code Playgroud)

这是我期望的和我想要的形状,但我想替换那些零.是什么造成的?

python arrays functional-programming python-3.x off-by-one

2
推荐指数
1
解决办法
65
查看次数